首页 > 其他 > 详细

行转列

时间:2019-07-23 10:46:07      阅读:73      评论:0      收藏:0      [点我收藏+]

新建表并插入数据:

create table t_grade

(name varchar2(50),

chinese_score number,

english_score number,

math_score number

);

insert into t_grade values (‘张三‘,90,81,80);

insert into t_grade values (‘李四‘,85,90,87);

commit;

select * from t_grade;

 技术分享图片

 

1、请把表格转换成下面的格式:

 技术分享图片

select name,‘语文‘ as subject,chinese_score as result from t_grade union

select name,‘英语‘ as subject,english_score as result from t_grade union

select name,‘数学‘ as subject,math_score as result from t_grade

order by name

 

 

以Oracle中scott的emp为例,统计各职位的人员在各部门的人数分布情况,就可以用“行转列”:

 技术分享图片

使用“行转列”统计各职位的人员在各部门的分布人数后,数据为:

select t.job, count(decode(t.deptno, ‘10‘, 1)) as "10(DEPTNO)",

       count(decode(t.deptno, ‘20‘, 1)) as "20(DEPTNO)",

       count(decode(t.deptno, ‘30‘, 1)) as "30(DEPTNO)",

       count(decode(t.deptno, ‘40‘, 1)) as "40(DEPTNO)"

  from scott.emp t

  group by t.job;

 技术分享图片

行转列

原文:https://www.cnblogs.com/chen8023miss/p/11229850.html

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