Concatenate grouped rows

Use:

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

Output:

----------- --------------------- ---------------------
1           40,00                 11,12
2           120,00                11

Leave a Comment