How to use GROUP BY to concatenate strings in SQL Server?

前端 未结 20 2579
无人及你
无人及你 2020-11-21 04:33

How do I get:

id       Name       Value
1          A          4
1          B          8
2          C          9

to

id               


        
20条回答
  •  不知归路
    2020-11-21 04:58

    An example would be

    In Oracle you can use LISTAGG aggregate function.

    Original records

    name   type
    ------------
    name1  type1
    name2  type2
    name2  type3
    

    Sql

    SELECT name, LISTAGG(type, '; ') WITHIN GROUP(ORDER BY name)
    FROM table
    GROUP BY name
    

    Result in

    name   type
    ------------
    name1  type1
    name2  type2; type3
    

提交回复
热议问题