简单的Oracle 行转列的sql语句

drop table ABC;   

create table ABC(id number,name varchar2(50),kcName varchar2(50),score number); 
insert into ABC values(1,'张三','语文',88); 
insert into ABC values(2,'张三','数学',68); 
insert into ABC values(3,'张三','英语',78); 
 
insert into ABC values(4,'李四','英语',78); 
insert into ABC values(5,'李四','语文',88); 
insert into ABC values(6,'李四','数学',79); 
 
insert into ABC values(7,'王五','英语',74); 
insert into ABC values(8,'王五','语文',58); 
insert into ABC values(9,'王五','数学',98); 
 
select * from abc; 
select abc.Name,sum(decode(kcName,'语文',SCORE,null)) 语文, 
                sum(decode(kcName,'数学',SCORE,null)) 数学, 
                sum(decode(kcName,'英语',SCORE,null)) 英语 
                from abc group by abc.name; 
 
结果:                 
NAME    语文  数学  英语 
1   王五  158 198 174 
2   李四  188 179 178 
3   张三  188 168 178

相关推荐