SQL Server-将列数据按分组拼接


相关示例代码

WITH sqlt
     AS (SELECT a.PlaceCode, 
                b.AcupointName
         FROM ITEM_Place_Correlation a
              LEFT JOIN ITEM_Acupoint b ON a.AcupointCode = b.AcupointCode
                                           AND b.isDeleted = 0),
     sqlt2
     AS (SELECT PlaceCode, 
                STUFF(
         (
             SELECT ', ' + AcupointName
             FROM sqlt
             WHERE(PlaceCode = it2.PlaceCode) FOR XML PATH(''), TYPE
         ).value('(./text())[1]', 'VARCHAR(MAX)'), 1, 2, '') AS AcupointNames
         FROM sqlt it2
         GROUP BY PlaceCode)
     SELECT *
     FROM sqlt2;

posted @ 2022-06-01 15:40  壮哈苗  阅读(1980)  评论(0编辑  收藏  举报