1. 程式人生 > >行列轉換之——多行轉多列,多列轉多行實踐版

行列轉換之——多行轉多列,多列轉多行實踐版

多行 max 演示 spa info 思想 .com 要求 列轉行

行列轉換之——多行轉多列,多列轉多行實踐版

1、多列轉行(核心思想,利用row_number() over() 來構造列傳行之後的唯一列,來行轉列)

  要求:

   技術分享圖片

實操演示:

select a as a,b as b,c as c
into #temp1
union all
select aa,bb,cc
union all 
select aaa,bbb,ccc

select * from 
(
    select column1,value,row_number() over(partition by column1 order
by value) as rn from #temp1 unpivot(value for column1 in(a,b,c) ) t ) a pivot(max(value) for rn in ([1],[2],[3])) t1

技術分享圖片



行列轉換之——多行轉多列,多列轉多行實踐版