我想知道是否可以从这张表中看出
ID Price ServiceID
1 50 11
1 -10 12
2 100 11
2 20 11
按 ID 分组,对价格求和并连接 serviceID,如下所示:
ID Price ServiceID
1 40 11,12
2 120 11
按 ID 分组并加总价格相当简单,但困扰我的是连接服务 ID。
感谢您的参与。
最佳答案
使用:
select t.id, sum(t.price)
, stuff((
select distinct ',' + cast(t2.ServiceID as varchar(max))
from @t t2
where t2.id = t.id
for xml path('')
), 1, 1, '')
from @t t
group by t.id
输出:
----------- --------------------- ---------------------
1 40,00 11,12
2 120,00 11
关于sql - 连接分组的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9139472/