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

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

hot3.png

一、行转列

1.1、初始测试数据
表结构:TEST_TB_GRADE
Sql代码:
1    create table TEST_TB_GRADE
2    (
3      ID        NUMBER(10) not null,
4      USER_NAME VARCHAR2(20 CHAR),
5      COURSE    VARCHAR2(20 CHAR),
6      SCORE     FLOAT
7    )
初始数据如下图:

Oracle行转列和列转行 - Magicc - 異次元藍客

  

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

Oracle行转列和列转行 - Magicc - 異次元藍客

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

Oracle行转列和列转行 - Magicc - 異次元藍客  

具体的实现sql如下:
Sql代码:
01    select t2.SCORE_GP,
02      sum(decode(t2.course, '语文', COUNTNUM,null)) as CHINESE,
03      sum(decode(t2.course, '数学', COUNTNUM,null)) as MATH,
04      sum(decode(t2.course, '英语', COUNTNUM,null)) as ENGLISH
05    from (
06      select t.course,
07             case when t.score  <60 then '00-60'
08                  when t.score >=60 and t.score <80  then '60-80'
09                  when t.score >=80 then '80-100' end as SCORE_GP,
10             count(t.score) as COUNTNUM
11      FROM test_tb_grade t
12      group by t.course, 
13            case when t.score  <60  then '00-60'
14                  when t.score >=60 and t.score <80  then '60-80'
15                  when t.score >=80 then '80-100' end
16      order by t.course ) t2
17    group by t2.SCORE_GP
18    order by t2.SCORE_GP
二、列转行
1.1、初始测试数据
表结构: TEST_TB_GRADE2 
Sql代码:  
1    create table TEST_TB_GRADE2
2    (
3      ID         NUMBER(10) not null,
4      USER_NAME  VARCHAR2(20 CHAR),
5      CN_SCORE   FLOAT,
6      MATH_SCORE FLOAT,
7      EN_SCORE   FLOAT
8    )
初始数据如下图:

Oracle行转列和列转行 - Magicc - 異次元藍客

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

Oracle行转列和列转行 - Magicc - 異次元藍客

这就是最常见的列转行,主要原理是利用SQL里面的union,具体的sql语句如下:

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

转载于:https://my.oschina.net/u/178116/blog/504628

你可能感兴趣的文章
最有趣的推理
查看>>
集合框架-Map
查看>>
python读取和生成excel文件
查看>>
我的友情链接
查看>>
tcpdump抓包 wireshark(ethereal)分析
查看>>
PHP 的 wordwrap 扩展功能
查看>>
ipsec的简单介绍及应用
查看>>
基于Eclipse的Hadoop应用开发环境配置
查看>>
分布式搜索方案选型
查看>>
Lync Server 2010的部署系列_第十九章 配置 Lync 2010 技能搜索
查看>>
我的友情链接
查看>>
RDS2016 Multipoint Role
查看>>
正则表达式口诀
查看>>
Linux 学习笔记_5_Linux引导流程解析_2_inittab文件剖析及系统启动流程分析
查看>>
Linux网络编程基础_2_物理层
查看>>
我的友情链接
查看>>
freebsd开启root远程登录服务器的操作
查看>>
未清销售订单及预测的所有工序物料需求(按BOM展开.考虑已发料)
查看>>
存储过程优点和缺点
查看>>
2.0、Android Studio编写你的应用
查看>>