博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle行列转换小结
阅读量:7308 次
发布时间:2019-06-30

本文共 2219 字,大约阅读时间需要 7 分钟。

目录结构如下:

行转列
列转行
[一]、行转列
 
1.1、初始测试数据
 
表结构:TEST_TB_GRADE
 
Sql代码 
create table TEST_TB_GRADE 
  ID        NUMBER(10) not null, 
  USER_NAME VARCHAR2(20 CHAR), 
  COURSE    VARCHAR2(20 CHAR), 
  SCORE     FLOAT 
 初始数据如下图:

                       

 
1.2、 如果需要实现如下的查询效果图:

                    

 
这就是最常见的行转列,主要原理是利用decode函数、聚集函数(sum),结合group by分组实现的,具体的sql如下:
Sql代码 
select t.user_name, 
  sum(decode(t.course, '语文', score,null)) as CHINESE, 
  sum(decode(t.course, '数学', score,null)) as MATH, 
  sum(decode(t.course, '英语', score,null)) as ENGLISH 
from test_tb_grade t 
group by t.user_name 
order by t.user_name 
 
 
1.3、延伸
 
如果要实现对各门功课的不同分数段进行统计,效果图如下:

                

 
具体的实现sql如下:
Sql代码 
select t2.SCORE_GP, 
  sum(decode(t2.course, '语文', COUNTNUM,null)) as CHINESE, 
  sum(decode(t2.course, '数学', COUNTNUM,null)) as MATH, 
  sum(decode(t2.course, '英语', COUNTNUM,null)) as ENGLISH 
from ( 
  select t.course, 
         case when t.score  <60 then '00-60' 
              when t.score >=60 and t.score <80  then '60-80' 
              when t.score >=80 then '80-100' end as SCORE_GP, 
         count(t.score) as COUNTNUM 
  FROM test_tb_grade t 
  group by t.course,  
        case when t.score  <60  then '00-60' 
              when t.score >=60 and t.score <80  then '60-80' 
              when t.score >=80 then '80-100' end 
  order by t.course ) t2 
group by t2.SCORE_GP 
order by t2.SCORE_GP 
 
[二]、列转行
 
1.1、初始测试数据
        表结构:TEST_TB_GRADE2
Sql代码 
create table TEST_TB_GRADE2 
  ID         NUMBER(10) not null, 
  USER_NAME  VARCHAR2(20 CHAR), 
  CN_SCORE   FLOAT, 
  MATH_SCORE FLOAT, 
  EN_SCORE   FLOAT 
 
        初始数据如下图:

       

 
1.2、 如果需要实现如下的查询效果图:

                      

 
这就是最常见的列转行,主要原理是利用SQL里面的union,具体的sql语句如下:
Sql代码 
select user_name, '语文' COURSE , CN_SCORE as SCORE from test_tb_grade2  
union select user_name, '数学' COURSE, MATH_SCORE as SCORE from test_tb_grade2  
union select user_name, '英语' COURSE, EN_SCORE as SCORE from test_tb_grade2  
order by user_name,COURSE  
 
 也可以利用【 insert all into ... select 】来实现,首先需要先建一个表TEST_TB_GRADE3:
Sql代码 
create table TEST_TB_GRADE3   
    (  
      USER_NAME VARCHAR2(20 CHAR),   
      COURSE    VARCHAR2(20 CHAR),   
      SCORE     FLOAT   
    )   
 再执行下面的sql:
 
Sql代码 
insert all 
into test_tb_grade3(USER_NAME,COURSE,SCORE) values(user_name, '语文', CN_SCORE) 
into test_tb_grade3(USER_NAME,COURSE,SCORE) values(user_name, '数学', MATH_SCORE) 
into test_tb_grade3(USER_NAME,COURSE,SCORE) values(user_name, '英语', EN_SCORE) 
select user_name, CN_SCORE, MATH_SCORE, EN_SCORE from test_tb_grade2; 
commit; 
 别忘记commit操作,然后再查询TEST_TB_GRADE3,发现表中的数据就是列转成行了。

 

转载地址:http://zodim.baihongyu.com/

你可能感兴趣的文章
更改DC时间每5分钟出现ID1030 1097|不要修改DC域控制器时间
查看>>
car认证中心配置
查看>>
yum 命令详解
查看>>
Linux内核模块(一)
查看>>
汇编总结:lea指令
查看>>
cobbler批量部署实验记录
查看>>
关于写日报
查看>>
我的友情链接
查看>>
新书试读_网络规划设计师考试考点分析与真题详解
查看>>
centos6.5安装openssh7.2p2方法
查看>>
ubuntu 13.04 root权限设置方法详解
查看>>
Iptables防火墙(一)
查看>>
使用nginx访问服务器log日志
查看>>
linux命令行抓取网页快照
查看>>
[免费赠票] 第九届中国云计算大会日程曝光
查看>>
完美spring boot 使用log4j2按级别输出到不同文件
查看>>
magento cron job
查看>>
Android Studio第二十六期 - 自定义Activity中Fragment之间的传值
查看>>
二、Windows Server 2008 R2 Hyper-V 创建虚拟机
查看>>
XPath 与 lxml
查看>>