Concatenate column values for rows with the same values (of different columns)

前端 未结 3 1443
余生分开走
余生分开走 2020-12-07 03:28

SQL Server 2005

I have a table which returns

ID  name    prop    value
--------------------------
1   one     Prop1   a
1   one     Prop1   b
1   one         


        
3条回答
  •  半阙折子戏
    2020-12-07 03:33

    SELECT ID
        ,name
        ,prop
        ,STUFF((SELECT ',' + value FROM table t2 WHERE t2.ID = t1.ID and t2.name = t1.name AND t2.prop = t1.prop FOR XML PATH('')),1,1,'') AS value
    FROM table t1
    GROUP BY ID,name,prop
    

    Please refer: SQL Query to get aggregated result in comma seperators along with group by column in SQL Server

提交回复
热议问题