如何在 pivot 的 IN 子句中添加 select 语句

how to add select statement in IN clause in pivot

iam 在 SQL 查询中使用 pivot table.. 我需要将一些列作为行,但我需要在 IN 子句中使用 select 语句,我们在其中给出选项这个

    PIVOT(
   sum(target)
    FOR collectionName IN (
 Select Ds.CollectionName as 'CollectionName'

from v_DeploymentSummary Ds

left join v_CIAssignment Vaa on Ds.AssignmentID = Vaa.AssignmentID

left join v_AuthListInfo LI on LI.ModelID = Ds.ModelID

Where Ds.FeatureType = 5 and Ds.CollectionName  not like '%Windows 8%'

and Li.Title like '%SUG_2020_06_P0_W7-W8-1_Critical%'
)) AS pivot_table

通常我们会给出这样的选项

    ) t
PIVOT(
   sum(target)
    FOR collectionName IN (
        [W7-8.1 - Ring 2 - Laptops Wave 5],
[W7-8.1 - Ring 2 - Laptops Wave 4],
[W7-8.1 - Ring 2 - Laptops Wave 3],
[W7-8.1 - Ring 2 - TARA -BI],
[W7-8.1 - Ring 2 - Desktops],
[W7-8.1 - Ring 2 - Laptops Wave 2],
[W7-8.1 - Ring 2 - Laptops Wave 1],
[W7 - Ring 1 - Early adopters],
[W7 - Ring 0 - Fast ring]
)) AS pivot_table

但是用 select 声明它给我错误?可以做什么。

使用这个我从一些 google 的帖子中得到了答案

select @cols = 
stuff( ( select distinct  ',[' + Ltrim(rtrim(Ds.CollectionName)) +']' from v_DeploymentSummary Ds
left join v_AuthListInfo LI on LI.ModelID = Ds.ModelID

Where Ds.FeatureType = 5 and Ds.CollectionName  not like '%Windows 8%'

and Li.Title like  @SUGname  FOR XML PATH('')),1,1,'');