行列转换之——多行转多列,多列转多行实践版

行列转换之——多行转多列,多列转多行实践版

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

 

猜你喜欢

转载自www.cnblogs.com/gered/p/10289013.html
今日推荐