我很难让此查询返回1行中的计数。 如果没有不同年份的交易(如本例中的2016年),结果显示为1行
但是当我插入不同年份的事务然后执行此代码时,结果显示2行具有相同的结果
这是代码
select
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=1)AND (YEAR(date)='2017'))),0) AS `Jan`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=2)AND (YEAR(date)='2017'))),0) AS `Feb`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=3)AND (YEAR(date)='2017'))),0) AS `Mar`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=4)AND (YEAR(date)='2017'))),0) AS `Apr`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=5)AND (YEAR(date)='2017'))),0) AS `May`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=6)AND (YEAR(date)='2017'))),0) AS `Jun`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=7)AND (YEAR(date)='2017'))),0) AS `Jul`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=8)AND (YEAR(date)='2017'))),0) AS `Aug`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=9)AND (YEAR(date)='2017'))),0) AS `Sep`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=10)AND (YEAR(date)='2017'))),0) AS `Oct`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=11)AND (YEAR(date)='2017'))),0) AS `Nov`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=12)AND (YEAR(date)='2017'))),0) AS `Dec`
from transaction GROUP BY YEAR(date)
我正在尝试在我的项目中制作年度报告,有没有办法解决这个或其他可以使其有效的查询?
答案 0 :(得分:1)
如果您只想要2017年的数据,则应在
中过滤此数据vswhere.exe
否则,对于您在数据中拥有的每一个,都会执行相同的ifnull(与2017相关)中的选择并返回相同的值
你可以避免使用unuseful()和subselect,例如:
select
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=1)AND (YEAR(date)='2017'))),0) AS `Jan`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=2)AND (YEAR(date)='2017'))),0) AS `Feb`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=3)AND (YEAR(date)='2017'))),0) AS `Mar`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=4)AND (YEAR(date)='2017'))),0) AS `Apr`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=5)AND (YEAR(date)='2017'))),0) AS `May`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=6)AND (YEAR(date)='2017'))),0) AS `Jun`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=7)AND (YEAR(date)='2017'))),0) AS `Jul`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=8)AND (YEAR(date)='2017'))),0) AS `Aug`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=9)AND (YEAR(date)='2017'))),0) AS `Sep`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=10)AND (YEAR(date)='2017'))),0) AS `Oct`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=11)AND (YEAR(date)='2017'))),0) AS `Nov`,
ifnull((SELECT COUNT(id) FROM (transaction)WHERE((Month(date)=12)AND (YEAR(date)='2017'))),0) AS `Dec`
from transaction
where YEAR(date)='2017'
GROUP BY YEAR(date)
答案 1 :(得分:1)
select
SUM(Month(date)=1) AND (YEAR(date)='2017') AS `Jan`,
SUM(Month(date)=2) AND (YEAR(date)='2017') AS `Feb`,
SUM(Month(date)=3) AND (YEAR(date)='2017') AS `Mar`,
SUM(Month(date)=4) AND (YEAR(date)='2017') AS `Apr`,
SUM(Month(date)=5) AND (YEAR(date)='2017') AS `May`,
SUM(Month(date)=6) AND (YEAR(date)='2017') AS `Jun`,
SUM(Month(date)=7) AND (YEAR(date)='2017') AS `Jul`,
SUM(Month(date)=8) AND (YEAR(date)='2017') AS `Aug`,
SUM(Month(date)=9) AND (YEAR(date)='2017') AS `Sep`,
SUM(Month(date)=10) AND (YEAR(date)='2017') AS `Oct`,
SUM(Month(date)=11) AND (YEAR(date)='2017') AS `Nov`,
SUM(Month(date)=12) AND (YEAR(date)='2017') AS `Dec`
from transaction
GROUP BY YEAR(date)
您可以尝试以上代码。
这里我只是在 SUM 函数中添加了条件。
如果此Month(date)=1) AND (YEAR(date)='2017'
条件为true,则它将返回1,否则返回0。
所以最后它会为你提供预期的输出。
它会帮助你。
答案 2 :(得分:1)
以下是通用查询。
SELECT COUNT(id), YEAR(created) as year, MONTH(created) as mon
FROM transaction
WHERE date IS NOT NULL
GROUP BY YEAR(date), MONTH(date) Desc
它会给出找到的所有月份的结果,以及您可以在代码中处理的缺失的月份。