加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sqlserver 表行转列

发布时间:2020-12-12 13:04:10 所属栏目:MsSql教程 来源:网络整理
导读: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,7unionselect 11,22,33,44,55,66,77--表中原始数据select * from test--行列互换with aa as(select *,id=ROW_NUMBER() OVER (ORDER
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,num7)
)AS unpvt2
where a1=a3

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读