Sql语句根据条件行并列求和的问题

数据库表中有如下数据
id type [count]
6 1 10
6 2 11
6 3 12
6 4 13
6 5 14
7 1 10
7 2 11
7 3 12
7 4 13
7 5 14
我要查询出的结果显示为
id [type 为1,2的值得和][type为3的值][type为4,5的值得和]
6 21 12 27
7 21 12 27
这个用sql语句怎么写,求高手!

select id
,sum(case when type in (1,2) then [count] else 0 end) as sum1
,sum(case when type in (3) then [count] else 0 end) as sum2
,sum(case when type in (4,5) then [count] else 0 end) as sum3
from 表名
group by id

温馨提示:答案为网友推荐,仅供参考
相似回答