Home >Database >Mysql Tutorial >简单的Oracle 行转列的sql语句

简单的Oracle 行转列的sql语句

WBOY
WBOYOriginal
2016-06-07 17:00:351015browse

drop table ABC; create table ABC(id number,name varchar2(50),kcName varchar2(50),score number); insert into ABC valu

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

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn