日期:2014-05-17  浏览次数:20417 次

多条件groupby的count语句,求大神帮助

数据如上图,我想要通过一个SQL语句,统计各projectId里不同chartype的个数,
还要统计typecode里1199的个数。大致格式如下:(里面的值都是乱填的)

不知道说得够不够清楚,如果不够清楚我还会补充,麻烦各位大哥了,
由于不能上传附件,所以数据我也没办法传上来,如果需要我可以发给你们。
再次感谢。
SQL GROUP?BY COUNT 求助

------解决方案--------------------
select projcetId,sum(case chartype when '网络' then 1 else 0 end) 网络,
sum(case when chartype='网络' and typecode=1199 then 1 else 0 end) 网络1199,
sum(case chartype when '地面' then 1 else 0 end) 地面,
sum(case when chartype='地面' and typecode=1199 then 1 else 0 end) 地面1199,
sum(case chartype when '网络QQ' then 1 else 0 end) 网络QQ,
sum(case when chartype='网络QQ' and typecode=1199 then 1 else 0 end) 网络QQ1199
from table
group by projcetId