DECLARE @strPivotColumns NVARCHAR(MAX)
SELECT
@strPivotColumns = STUFF((SELECT DISTINCT ', ' + item
FROM table
WHERE id = '9972'
FOR XML PATH('')), 1, 1, '')
FROM
table
GROUP BY
item
SELECT DISTINCT
column1,
.....
column10,
@strPivotColumns
FROM
table1
LEFT OUTER JOIN
table2 ON table2.id = table1.id
上述查询的原始结果
column1|column2|@strPivotColumns column I want to change
abc | def | y,z,x....N
我期望的结果
column1|column2|Address1|.......|AddressN|
abc | def| y |.......| N |
我花了很多时间来寻找关键示例,并浏览了许多示例,但没有找到适合我的情况的案例。