2017-10-28 178 views
0

这是我的源表(pivot_dummy):SQL支点 - 如何获得列之间的所有可能的组合摆动

Source data Table

,我需要通过Parameter_type转动,但需要Parameter_val的之间的所有可能的组合。把它做这样

Desired Output

什么我用这个代码:

SELECT nct_id, [Asset],[Indication], rowid 
FROM (SELECT nct_id,Parameter_val,parameter_type, rowid 
     FROM (Select *, 
        Row_Number() Over (Partition By nct_id,Parameter_type ORDER BY nct_id) RowId 
      from [dbo].[pivot_dummy] 
      ) a 
    ) s 
Pivot (
    max(parameter_val) 
     for Parameter_type in ([Asset], [Indication]) 
    ) as pivottable 

但是这个代码的结果如下:

Current Output

是否有人可以帮忙吗?

回答

0

从我可以告诉,你根本不想要pivot。简单的一个join

select pd1.nct_id, pd1.parameter_value as asset, pd2.parameter_value as indication 
from pivot_dummy pd1 join 
    pivot_dummy pd2 
    on pd1.nct_id = pd2.nct_id and 
     pd1.parameter_type = 'Asset' and 
     pd2.parameter_type = 'Indication'; 
+0

嗨戈登,这工作很好!非常感谢! –

+0

嗨戈登,当只有两列(资产和指示)时,这解决了我的问题。但实际上,我有7列需要进行调整,并且还可以根据NCTID找到所有可能的组合,可能的解决方案是什么?我还应该考虑摆动吗? –

+0

@AnkurBansal。 。 。只能回答你所问的问题。如果您有其他问题,请将其作为*新问题。 –

相关问题