create table test(num1 int,num2 int,num3 int,num4 int,num5 int,num6 int,num7 int)
insert into test
select 1,2,3,4,5,6,7
union
select 11,22,33,44,55,66,77
--表中原始数据
select * from test
--行列互换
with aa as(
select *,id=ROW_NUMBER() OVER (ORDER BY num1) from test
)
SELECT a2,a4
FROM
(select * from aa where id=1) p
UNPIVOT
(a2 FOR a1 IN
(num1, num2, num3, num4, num5 , num6, num7)
)AS unpvt1,
(select * from aa where id=2) p
UNPIVOT
(a4 FOR a3 IN
(num1, num2, num3, num4, num5 , num6, num7)
)AS unpvt2
where a1=a3
sqlserver 表行转列
最新推荐文章于 2023-02-01 14:32:39 发布