我将简化这一点:
我有两个可以正常工作的 SQL 表达式:
第一的:
select count(*) as number1
from T1
where DATE1>'2012-01-01' and DATE2<'2012-12-31'
结果:13
select count(*) as number2
from T1
where DATE3>DATE2 and CURDATE()>DATE2
结果:5
但是当我尝试在 GROUP BY 中插入这两个 COUNTS 时,我得到的结果总是 13 !!!
SELECT NAME,
COUNT(case when DATE1>'2012-01-01' and DATE2<'2012-12-31' then 1 else 0 end) as number1,
COUNT (case when (DATE3>DATE2 and CURDATE()>DATE2) then 1 else 0 end) as number 2
from T1
我得到的结果是:
NAME NUMBER1 NUMBER2
A 5 5
B 4 4
C 4 4
但我应该得到:
NAME NUMBER1 NUMBER2
A 5 4
B 4 0
C 4 1
这样列的总和为 13 和 5,就像前两个查询一样。我究竟做错了什么?谢谢