簡體   English   中英

SQL數據透視-如何獲取要透視的列之間的所有可能組合

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

這是我的源表(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';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM