Optimal way to concatenate/aggregate strings

前端 未结 7 1338
[愿得一人]
[愿得一人] 2020-11-22 11:17

I\'m finding a way to aggregate strings from different rows into a single row. I\'m looking to do this in many different places, so having a function to facilitate this woul

7条回答
  •  清歌不尽
    2020-11-22 11:52

    For those of us who found this and are not using Azure SQL Database:

    STRING_AGG() in PostgreSQL, SQL Server 2017 and Azure SQL
    https://www.postgresql.org/docs/current/static/functions-aggregate.html
    https://docs.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql

    GROUP_CONCAT() in MySQL
    http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

    (Thanks to @Brianjorden and @milanio for Azure update)

    Example Code:

    select Id
    , STRING_AGG(Name, ', ') Names 
    from Demo
    group by Id
    

    SQL Fiddle: http://sqlfiddle.com/#!18/89251/1

提交回复
热议问题