首页 > 数据库技术 > 详细

SQL行转列

时间:2015-11-20 19:06:49      阅读:302      评论:0      收藏:0      [点我收藏+]

建表:

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;

SQL行转列

原文:http://www.cnblogs.com/lxaic/p/4981392.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!