Merge multiple rows into one column without duplicates

后端 未结 4 736
轻奢々
轻奢々 2021-02-01 02:44

I am working on a query that will collect data from a table and display the data for a report.

The data looks like this:

Player Score
001      10
001           


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 03:45

    Previous accepted answer is superseded in SQL 2017 and up by STRING_AGG:

    SELECT Player, STRING_AGG(Score,', ') FROM YourTable GROUP BY Player
    

    No need to use awkward FOR XML syntax.

    I ran this and the accepted answer side-by-side on 100K rows. Accepted answer took 90 seconds, the STRING_AGG version takes less than 1 second.

提交回复
热议问题