SQL中关于条件Count的查询语句问题
createtableabc(Aint,Bint)SelectA,count(B)astotalfromABCgroupbyASelectA,count(B)astota...
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 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
...... 展开
2个回答
展开全部
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
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
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询