SQL支点 - 如何获得列之间的所有可能的组合摆动
问题描述:
这是我的源表(pivot_dummy):SQL支点 - 如何获得列之间的所有可能的组合摆动
,我需要通过Parameter_type转动,但需要Parameter_val的之间的所有可能的组合。把它做这样
什么我用这个代码:
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
但是这个代码的结果如下:
是否有人可以帮忙吗?
答
从我可以告诉,你根本不想要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';
嗨戈登,这工作很好!非常感谢! –
嗨戈登,当只有两列(资产和指示)时,这解决了我的问题。但实际上,我有7列需要进行调整,并且还可以根据NCTID找到所有可能的组合,可能的解决方案是什么?我还应该考虑摆动吗? –
@AnkurBansal。 。 。只能回答你所问的问题。如果您有其他问题,请将其作为*新问题。 –