IT忍者神龟之 oracle行转列、列转行
2016-04-04 11:29
507 查看
一、行转列
须要将例如以下格式
转换为:
这就是最常见的行转列,主要原理是利用decode函数、聚集函数(sum)。结合group by分组实现的
[sql]
view plaincopy
create table test(
id varchar2(255) primary key not null,
name varchar2(255),
course varchar2(255),
score varchar2(255)
);
insert into test values(sys_guid(),'zhangsan','语文',85);
insert into test values(sys_guid(),'zhangsan','数学',78);
insert into test values(sys_guid(),'zhangsan','英语',90);
insert into test values(sys_guid(),'lisi','语文',73);
insert into test values(sys_guid(),'lisi','数学',84);
insert into test values(sys_guid(),'lisi','英语',92);
行转列SQL语句为:
[sql]
view plaincopy
select t.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 t
group by t.name
order by t.name
二、列转行
将例如以下格式
转换为
这就是最常见的列转行。主要原理是利用SQL里面的union
[sql]
view plaincopy
create table test(
id varchar2(255) primary key not null,
name varchar2(255),
ch_score varchar2(255),
math_score varchar2(255),
en_score varchar2(255)
);
insert into test values(sys_guid(),'zhangsan',88,76,90);
insert into test values(sys_guid(),'lisi',91,67,82);
列转行SQL语句为:
[sql]
view plaincopy
select name, '语文' COURSE , ch_score as SCORE from test
union select name, '数学' COURSE, MATH_SCORE as SCORE from test
union select name, '英语' COURSE, EN_SCORE as SCORE from test
order by name,COURSE
须要将例如以下格式
转换为:
这就是最常见的行转列,主要原理是利用decode函数、聚集函数(sum)。结合group by分组实现的
[sql]
view plaincopy
create table test(
id varchar2(255) primary key not null,
name varchar2(255),
course varchar2(255),
score varchar2(255)
);
insert into test values(sys_guid(),'zhangsan','语文',85);
insert into test values(sys_guid(),'zhangsan','数学',78);
insert into test values(sys_guid(),'zhangsan','英语',90);
insert into test values(sys_guid(),'lisi','语文',73);
insert into test values(sys_guid(),'lisi','数学',84);
insert into test values(sys_guid(),'lisi','英语',92);
行转列SQL语句为:
[sql]
view plaincopy
select t.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 t
group by t.name
order by t.name
二、列转行
将例如以下格式
转换为
这就是最常见的列转行。主要原理是利用SQL里面的union
[sql]
view plaincopy
create table test(
id varchar2(255) primary key not null,
name varchar2(255),
ch_score varchar2(255),
math_score varchar2(255),
en_score varchar2(255)
);
insert into test values(sys_guid(),'zhangsan',88,76,90);
insert into test values(sys_guid(),'lisi',91,67,82);
列转行SQL语句为:
[sql]
view plaincopy
select name, '语文' COURSE , ch_score as SCORE from test
union select name, '数学' COURSE, MATH_SCORE as SCORE from test
union select name, '英语' COURSE, EN_SCORE as SCORE from test
order by name,COURSE
相关文章推荐
- 用TP对Oracle数据库过程问题
- ORACLE directory 目录
- win7 64位操作系统中 Oracle 11g 安装教程(图解)
- 解决Navicat无法连接Oracle的问题
- oracle安装完成后没有tnsnames.ora和listener.ora问题方法
- oracle10g,提示tns:无监听程序的问题解决
- 甲骨文的 Linux 部门主管 Coekaerts 跳槽到微软
- Oracle12c新特性pdborcl,如何登录到普通用户scott
- EF+Oracle+Unity+MVC+AutoMapper 框架的搭建
- oracle 序列 详解
- 压测调优之遇到的proxool问题
- Oracle SQL 查询优化.Part2
- oracle开发人员误删表的操作及恢复
- Oracle SQL 查询优化.Part1
- Oracle学习笔记五 --- 表查询
- Oracle学习笔记四 --- 变量及表管理
- Oracle学习笔记三 --- 用户管理
- Oracle dataguard之备库切换(failover)
- Oracle-05-SQL语句概述、分类&SQL*PLUS概述(初识insert,desc,list,r,del,a,c,n等命令)
- Oracle dataguard之主备库切换(switchover)