SQL行转列

建表:

create table studentinfo(

name varchar(20) ,
subject varchar(20),
score number(3,1));

插入记录:

insert into studentinfo values('张三','语文',69);

insert into studentinfo values('张三','数学',70);

insert into studentinfo values('李四','语文',20);

里面的记录为:

我们想要的结果为:

通过下面的sql可以实现:

select a 姓名,max(a1) 语文,max(a2) 数学
from (
    select a, case when b = '语文' then c else null end as a1,
          case when b = '数学' then c else null end as a2
    from
    (select name a,subject b,score c from studentinfo) x
) y
group by a;

作者:caobotao
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
posted @ 2015-11-20 16:55  caobotao  阅读(243)  评论(0编辑  收藏  举报