首页 > 数据库技术 > 详细

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

时间:2016-08-26 15:28:24      阅读:280      评论:0      收藏:0      [点我收藏+]

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

 

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

原文:http://www.cnblogs.com/xiao-momo/p/5810335.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!