sql

来源:互联网 发布:火车票查询软件 编辑:程序博客网 时间:2024/06/09 14:19

create table tb(name char(50),xueke char(50),chengji int)
insert into tb values('张三','语文',90);
insert into tb values('张三','数学',80);
insert into tb values('张三','英文',70);
insert into tb values('李四','语文',99);
insert into tb values('李四','数学',88);
insert into tb values('李四','英文',77);
insert into tb values('王五','语文',89);
insert into tb values('王五','数学',87);
insert into tb values('王五','英文',86);

declare @sql varchar(8000)
set @sql='select a.name'
select @sql=@sql+',max(case when b.xueke='''+xueke+''' then b.chengji else null end) ['+xueke+']'
from tb group by xueke

exec (@sql+' from tb a left join tb b on a.name=b.name group by a.name')


drop table tb 

原创粉丝点击