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

lk1822 2013-01-26 03:13:10

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

不知道说得够不够清楚,如果不够清楚我还会补充,麻烦各位大哥了,
由于不能上传附件,所以数据我也没办法传上来,如果需要我可以发给你们。
再次感谢。
...全文
277 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dj2437 2013-01-26
  • 打赏
  • 举报
回复
横竖交叉,我之前也不会
lk1822 2013-01-26
  • 打赏
  • 举报
回复
太好了,就是这个效果,非常感谢!!!
dong_y888 2013-01-26
  • 打赏
  • 举报
回复
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

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧