1.多行转成一行(并以','分开)
表数据如下图:
查询结果如下图:
SQL查询脚本:
SELECT addPer, house_code = (STUFF((SELECT ',' + house_code FROM my_demo WHERE addPer = T.addPer FOR XML PATH('')), 1, 1, '')) FROM my_demo AS T GROUP BY addPer
2.一行转为一列
SELECT STUFF((SELECT ',' + CAST(id AS VARCHAR(10)) FROM(SELECT id FROM myDemo WHERE del = 0 ) a FOR XML PATH('') ), 1,0, '') AS Name