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



iam在SQL查询中使用透视表。。我需要将一些列设置为rowss,但我需要在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语句给了我错误??可以做些什么。

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

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,'');

最新更新