您好,欢迎访问一九零五行业门户网

SQL 内置函数(pivot) 纵转横

1.创建表: create table test(id int,name varchar(20),quarter int,profile int)) insert into test values(1,
1.创建表:
create table test(id int,name varchar(20),quarter int,profile int))
insert into test values(1,'a',1,1000);
insert into test values(1,'a',2,1000);
insert into test values(1,'a',3,1000);
insert into test values(1,'a',4,1000);
insert into test values(2,'a',1,1000);
insert into test values(2,'a',2,1000);
insert into test values(2,'a',3,1000);
insert into test values(2,'a',4,1000);
2. pivot 用法
select id,name,[1] as '1th season',[2] as '2th season',[3] as '3th season',[4] as '4th season'
from test
pivot
(
sum(profile)
for quarter in ([1],[2],[3],[4])
)as pvt
select * from test
3.unpivot
select * from test2
select id,name,quarters,profiles
from test2
unpivot
(
profiles
for quarters in ([q1],[q2],[q3],[q4])
)
as unpvt

其它类似信息

推荐信息