sql统计某一字段等于不同值的个数
sql
比如有这一张表t,数据如下
id type 001 1 001 0 002 1 001 0 002 0 001 1 001 0 002 0
现在要统计不同id,type分别为0的,1的个数,查询语句如下
select id,sum(case when type=0 then 1 else 0 end) as 0,sum(case when type=1 then 1 else 0 end) as 1 from t group by id
查询结果如下
id 0 1 001 3 2 002 2 1