在SQL Server 中動態旋轉多個欄位
問題:
問題:解決方案:
要實現多列動態旋轉,請依照下列步驟操作:
select id, col = cast(t_year as varchar(4))+'_'+t_type+'_'+col, value from ATM_TRANSACTIONS t cross apply ( select 'total', total union all select 'volume', volume ) c (col, value);在我們的範例中,我們要取消透視「總計」和「數量」」欄位:
select ID, [2008_A_total], [2008_A_volume], [2008_B_total], [2008_B_volume], [2008_C_total], [2008_C_volume], [2009_A_total], [2009_A_volume] from ( select id, col = cast(t_year as varchar(4))+'_'+t_type+'_'+col, value from ATM_TRANSACTIONS t cross apply ( select 'total', total union all select 'volume', volume ) c (col, value) ) d pivot ( max(value) for col in ([2008_A_total], [2008_A_volume], [2008_B_total], [2008_B_volume], [2008_C_total], [2008_C_volume], [2009_A_total], [2009_A_volume]) ) piv;
輸出:
以上是如何在 SQL Server 中對多個欄位執行動態透視?的詳細內容。更多資訊請關注PHP中文網其他相關文章!