数据库 行转列 列转行详解
来源:互联网 发布:大数据分析算法及模型 编辑:程序博客网 时间:2024/06/05 00:33
目录结构如下:
- 行转列
- 列转行
[一]、行转列
1.1、初始测试数据
表结构:TEST_TB_GRADE
- (
- ID NUMBER(10) not null,
- USER_NAME VARCHAR2(20 CHAR),
- COURSE VARCHAR2(20 CHAR),
- SCORE FLOAT
- )
- 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如下:
- 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
- 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如下:
- 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
- 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
- (
- ID NUMBER(10) not null,
- USER_NAME VARCHAR2(20 CHAR),
- CN_SCORE FLOAT,
- MATH_SCORE FLOAT,
- EN_SCORE FLOAT
- )
- 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语句如下:
- 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
- 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:
- (
- USER_NAME VARCHAR2(20 CHAR),
- COURSE VARCHAR2(20 CHAR),
- SCORE FLOAT
- )
- create table TEST_TB_GRADE3
- (
- USER_NAME VARCHAR2(20 CHAR),
- COURSE VARCHAR2(20 CHAR),
- SCORE FLOAT
- )
再执行下面的sql:
- 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;
- 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,发现表中的数据就是列转成行了。
0 0
- 数据库 行转列 列转行详解
- 数据库 行转列 列转行详解
- 数据库 行转列 列转行详解
- 数据库 行转列 列转行详解
- oracle数据库 行转列 列转行详解
- [面试][oracle] 数据库 行转列 列转行详解
- 数据库行转列和列转行小例子
- 列转行、行转列
- 行转列 -列转行
- SQL行转列 列转行
- SQL行转列,列转行
- 行转列 列转行
- 行转列.列转行 oracle
- oracle 行转列,列转行
- 【ORACLE】行转列,列转行
- SQL 列转行/行转列
- oracle 行转列 列转行
- oracle 行转列,列转行
- 一些面试可能遇到的基础知识
- Android 批量设置监听器,监听器传递参数,重写监听器构造函数
- c语言 异常捕捉 setjmp & longjmp
- Hotelling transform,霍特林变换
- UDP校验和计算
- 数据库 行转列 列转行详解
- 孙鑫MFC消息映射机制的剖析
- 交叉工具链
- 开发 Servlet 三种方法
- 苹果新贵 Swift 之前世今生
- ThinkPHP中volist标签的使用
- 【LaTeX排版】LaTeX论文排版<三>
- 学习OpenCV范例(二十四)—ViBe前景检测(二)
- 孙鑫MFC笔记MFC消息映射机制的剖析