这是正常的mysql查询
SELECT SUM( Minutes ) as minsDecPrac, COUNT( DISTINCT (CAST(dtDateTime as DATE))) as playDecPrac FROM `tbl_atschool_timelog` WHERE `intuid` = 48876 AND `intGametypeId` = 2 AND `intGame` = 1
这是输出
minsDecPrac playDecPrac
17 1
这是使用CASE WHEN的查询
SELECT SUM(CASE WHEN intGametypeId = 2 AND intGame =1 THEN Minutes ELSE 0 END) AS minsDecPrac COUNT(DISTINCT CASE WHEN intGametypeId = 2 AND intGame =1 THEN (CAST(dtDateTime as DATE)) ELSE 0 END) AS playDecPrac FROM `tbl_atschool_timelog` WHERE `intuid` = 48876
这是输出
minsDecPrac playDecPrac
17 2
问题是,如果我使用CASE WHEN,则“ playDecPrac”(dtDateTime)的输出是错误的。
我不知道我错了。如果有人知道,请帮助我
最佳答案
此子句在这里:
COUNT(DISTINCT CASE WHEN intGametypeId = 2 AND intGame =1 THEN (CAST(dtDateTime as DATE)) ELSE 0 END)
除日期外,还将
ELSE 0
中的0计数为一个不同的值。因此COUNT(DISTINCT )
比应有的值高一个。您可以尝试
ELSE NULL
。