SQL Query for aggregation/concatenation

前端 未结 1 1876
孤独总比滥情好
孤独总比滥情好 2021-01-22 14:39

I have a table like this:

ID    Name
----------
1   john
1   molly
2   greg
2   sean
1   holly
2   mill

What should the SQL Query be to aggrega

1条回答
  •  囚心锁ツ
    2021-01-22 15:13

    Note: The STUFF function simply removes the leading / from the string returned.

    SELECT t1.id, 
           STUFF((SELECT '/' + t2.name
                    FROM YourTable t2
                    WHERE t1.id = t2.id
                    ORDER BY t2.name
                    FOR XML PATH('')),1,1,'') AS Name
        FROM YourTable t1
        GROUP BY t1.id
    

    0 讨论(0)
提交回复
热议问题