简体   繁体   English

SQL数据透视-如何获取要透视的列之间的所有可能组合

[英]SQL pivot - How to get all possible combinations between columns to be pivoted

This is my source table (pivot_dummy): 这是我的源表(pivot_dummy):

源数据表

and I need to pivot it by Parameter_type but need all possible combinations between Parameter_val's. 并且我需要按Parameter_type进行旋转,但需要Parameter_val之间的所有可能组合。 Something like this 像这样

期望的输出

I am using this code to get it done: 我正在使用此代码来完成它:

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

but this code results in following: 但是此代码导致以下结果:

电流输出

Can someone please help? 有人可以帮忙吗?

From what I can tell, you don't want a pivot at all. 据我所知,您根本不需要pivot Simply a join : 只需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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM