网友嫌弃俺用SUM,不用Count 。俺就举了这个例子。sum有时很方便,可以一个select 完成多个数据的统计。
declare @t table(usercode varchar(20),sex varchar(1) ,age int ,intern int ,city varchar(20) )
insert @t(usercode,sex,age,intern,city) values('001',1,55,0,'BJ')
insert @t(usercode,sex,age,intern,city) values('002',1,40,0,'BJ')
insert @t(usercode,sex,age,intern,city) values('003',1,24,0,'BJ')
insert @t(usercode,sex,age,intern,city) values('004',0,32,1,'SH')
select city as [城市] ,
count(1) as [人数] ,
sum(case when sex=0 then 1 else 0 end) as [人数(男)],
sum(case when sex=1 then 1 else 0 end) as [人数(女)],
sum(case when age>=45 then 1 else 0 end) as [人数(>=45岁)],
sum(case when intern=1 then 1 else 0 end) as [实习生数]
from @t group by city