天天看点

sql行列互换问题

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