Maison > base de données > tutoriel mysql > oracle decode 函数实现行转列

oracle decode 函数实现行转列

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2016-06-07 14:55:19
original
1412 Les gens l'ont consulté

用decode函数,或者case when实现行转列 Oracle ----创建测试表create table student_score( name varchar2(20), subject varchar2(20), score number(4,1) ); -----插入测试数据insert into student_score (name,subject,score)values('张三','语文',78); in

用decode函数,或者case when实现行转列
Oracle
----创建测试表

create table student_score(

       name varchar2(20),

       subject varchar2(20),

       score number(4,1)

 

);

 

-----插入测试数据

insert into student_score (name,subject,score)values('张三','语文',78);

 

insert into student_score (name,subject,score)values('张三','数学',88);

 

insert into student_score (name,subject,score)values('张三','英语',98);

 

 

insert into student_score (name,subject,score)values('李四','语文',89);

 

insert into student_score (name,subject,score)values('李四','数学',76);

 

insert into student_score (name,subject,score)values('李四','英语',90);

 

 

insert into student_score (name,subject,score)values('王五','语文',99);

 

insert into student_score (name,subject,score)values('王五','数学',66);

 

insert into student_score (name,subject,score)values('王五','英语',91);

 

-----decode行转列

select name "姓名",

       sum(decode(subject, '语文', nvl(score, 0), 0)) "语文",

       sum(decode(subject, '数学', nvl(score, 0), 0)) "数学",

       sum(decode(subject, '英语', nvl(score, 0), 0)) "英语"

  from student_score

 group by name;

 

------ case when 行转列

 

select name "姓名",

       sum(case when subject='语文'

       then nvl(score,0)

       else 0

       end) "语文",

       sum(case when subject='数学'

       then nvl(score,0)

       else 0

       end) "数学",

       sum(case when subject='英语'

       then nvl(score,0)

       else 0

       end) "英语"

      from student_score

       group by name;
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal