【问题标题】:Display the monthwise financial year records显示每月财政年度记录
【发布时间】:2021-01-03 20:01:21
【问题描述】:

我使用下面的查询来获取计数、月份名称和金额,但下面的查询显示数据库中可用的所有记录。我只需要显示财政年度。就像我必须显示从01-04-202031-03-2021 的数据。

我想知道我必须更改什么查询?

SELECT COUNT(f_id) as count
     , MONTHNAME(date_of_created) as month_name
     , sum(f_amt) as amount 
  FROM tbl_fileStatus 
 WHERE f_filestatus=1  
 GROUP 
    BY YEAR(date_of_created)
     , MONTH(date_of_created)

【问题讨论】:

标签: mysql sql


【解决方案1】:

只需在 WHERE 子句中添加日期条件:

SELECT COUNT(f_id) as count,
       MONTHNAME(date_of_created) as month_name,
       SUM(f_amt) as amount 
FROM tbl_fileStatus 
WHERE  f_filestatus=1  
  AND date_of_created BETWEEN '2020-04-01' AND '2021-03-31'
GROUP BY YEAR(date_of_created),MONTH(date_of_created)

要使用动态年份,您可以执行以下操作:

SET @year = 2020
;
SELECT COUNT(f_id) as count,
       MONTHNAME(date_of_created) as month_name,
       SUM(f_amt) as amount 
FROM tbl_fileStatus 
WHERE  f_filestatus=1  
  AND date_of_created BETWEEN CONCAT(@year, '-04-01') and CONCAT(@year+1, '-03-31')
GROUP BY YEAR(date_of_created),MONTH(date_of_created)

如果您想在参数中传递年份,您可以在上述查询中将@year 替换为占位符。

请注意,如果您将日期时间段限制为一年或更短,则无需按年分组,只需按月分组。

【讨论】:

  • 如何设置年份动态?我的意思是我不想手动更改年份
  • @user9437856 看到我的编辑,这是你的意思吗?或者如果你想传入一个参数,你可以用占位符替换@year
  • 给我一些时间检查查询
  • 是的,它正在工作。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多