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
测试查询语句
Select send_id as sendId,accept_id as acceptId,
count(sign_flag) as numAll,
sum(case when sign_flag = 1 then 1 else 0 end) as numOk,
sum(case when sign_flag = 0 then 1 else 0 end) as numNo
from msg_chat WHERE accept_id = '1099949883273646082' group by send_id
以下是查询结果