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

uurv41yg  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(615)

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语句给了我错误??我们能做什么。

h6my8fg2

h6my8fg21#

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

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

相关问题