建表语句
create table A( ID int primary key identity(1,1) , Name varchar(20), Course varchar(20), Score varchar(20) ) go insert into A values('张三','语文','75') insert into A values('张三','数学','80') insert into A values('张三','英语','90') insert into A values('李四','语文','95') insert into A values('李四','数学','55')
纵表转横表
select name, sum( case when course = '语文' then score else 0 end ) as '语文', sum( case when course = '数学' then score else 0 end ) as '数学', sum( case when course = '英语' then score else 0 end ) as '英语' from A group by name