level 3
--建表
create table [Scores]
(
[ID] int identity(1,1) primary key,
[Student] varchar(20) ,
[Subject] varchar(30),
[Score] float
)
--插入记录
insert into Scores(Student,Subject,Score) values('张三','语文','90')
--行列互换
/*
pivot(
聚合函数(要转成列值的列名)
for 要转换的列
in(目标列名)
)
*/
select Student as '姓名',
avg(语文) as '语文',
avg(英语) as '英语',
avg(数学) as '数学'
from Scores
pivot(
avg(Score) for Subject
in (语文,英语,数学)
)as NewScores
group by Student
order by Student asc

2021年04月27日 10点04分
1
create table [Scores]
(
[ID] int identity(1,1) primary key,
[Student] varchar(20) ,
[Subject] varchar(30),
[Score] float
)
--插入记录
insert into Scores(Student,Subject,Score) values('张三','语文','90')
--行列互换
/*
pivot(
聚合函数(要转成列值的列名)
for 要转换的列
in(目标列名)
)
*/
select Student as '姓名',
avg(语文) as '语文',
avg(英语) as '英语',
avg(数学) as '数学'
from Scores
pivot(
avg(Score) for Subject
in (语文,英语,数学)
)as NewScores
group by Student
order by Student asc

