Concatenate grouped rows

前端 未结 1 1652
逝去的感伤
逝去的感伤 2020-11-30 14:58

I would like to know is it possible from this table

ID   Price    ServiceID
1    50       11
1    -10      12
2    100      11
2    20       11
相关标签:
1条回答
  • 2020-11-30 15:49

    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
    
    0 讨论(0)
提交回复
热议问题