例如,根据字段stat=1和=2分别按时间统计的结果:
date count1(stat=1) count2(stat=2)
--------------------------------------------
2005-7-1 1 4
2005-7-2 8 0
2005-7-3 2 9
...... .... .....
select
[date],
count1 = sum(case when stat=1 then 1 else 0 end),
count2 = sum(case when stat=2 then 1 else 0 end)
from
表
group by
[date]
order by
[date]
select
[term],
sum(case when stat=1 then 1 else 0 end) as count1(stat=1) ,
sum(case when stat=2 then 1 else 0 end) as count2(stat=2)
from
表名
group by
[term]