create table tb_student --create table
(
sname varchar(20),
object varchar (20),
score int
)
go
insert tb_student select 'a','EN',89 union all --add data to table
select 'a','CH',78 union all
select 'a','HO',99 union all
select 'b','EN',34 union all
select 'b','CH',88 union all
select 'b','HO',0
go
select * from tb_student --view data sourse
select sname,英語=sum(case
object when 'EN' then score
end),國文=sum(case
object when 'CH' then score
end),數學=sum(case
object when 'HO' then score
end)
from tb_student group by sname