create database Hill_test;
go
use Hill_test;
go
create table stu_info (
id int primary key identity(1,1),
name varchar(30) not null,
subject varchar(30) not null,
score int not null
)
insert into stu_info values('张三','chinese',77);
insert into stu_info values('张三','math',78);
insert into stu_info values('张三','english',79);
insert into stu_info values('李四','chinese',87);
insert into stu_info values('李四','math',88);
insert into stu_info values('李四','englisg',89);
select * from stu_info;
select name, sum(b.语文)as '语文' ,sum(b.数学) as '数学',sum(b.英语) as '英语' from (
select name ,
case t.subject when 'chinese' then t.score else 0 end as '语文',
case t.subject when 'math' then t.score else 0 end as '数学',
case t.subject when 'english' then t.score else 0 end as '英语'
from stu_info t
) b group by name