TSQL- csv for a column and not for the rest

前端 未结 3 1503
说谎
说谎 2021-01-25 16:14

i need to get csv from a column but the minimum value from the rest of the columns (or any value because they are same for a group). As an example;

I have the following

3条回答
  •  花落未央
    2021-01-25 16:52

    Use STUFF with GROUP BY.

    Query

    SELECT STUFF((SELECT ',' + [COL1] 
    FROM [your_table_name]
    WHERE ([COL2] = t.[COL2] AND [COL3] = t.[COL3] AND [COL4] = t.[COL4]) 
    FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
       ,1,1,'') AS COL1, [COL2], [COL3], [COL4]
    FROM [your_table_name] t
    GROUP BY [COL2], [COL3], [COL4]
    

提交回复
热议问题