SQL中关于条件Count的查询语句问题

create table abc(A int,B int) 

Select A,count(B) as total from ABC group by A
Select A,count(B) as total1 from ABC where B > 30 group by A
Select A,count(B) as totlal2 from ABC where B > 20 group by A

 

如何合并这三个查询?
得到一个查询结果:
A,total,total1,total2

 

答:

Select A,
count(B) as total,
sum(case when B > 30 then 1 else 0 end) as total1,
sum(case when B > 20 then 1 else 0 end) as total2 
from ABC group by A

 

posted @ 2016-08-26 14:49  小_默默  阅读(18212)  评论(0编辑  收藏  举报