SQL pivot - 如何获得要旋转的列之间的所有可能组合
SQL pivot - How to get all possible combinations between columns to be pivoted
这是我的来源table (pivot_dummy):
并且我需要按 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';
这是我的来源table (pivot_dummy):
并且我需要按 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';