IT忍者神龟之 oracle行转列、列转行
2014-10-09 16:28
176 查看
一、行转列
需要将如下格式
转换为:
这就是最常见的行转列,主要原理是利用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
相关文章推荐
- IT忍者神龟之 oracle行转列、列转行
- IT忍者神龟之oracle获取本周、本月、本季度、本年度的第一天和最后一天 (备查)
- IT忍者神龟之Oracle 的数据导入导出及 Sql Loader (sqlldr) 的用法
- IT忍者神龟之Oracle查询树形结构
- IT忍者神龟之mysql与oracle的对比
- IT忍者神龟之Oracle PL/SQL 调用Java Source
- IT忍者神龟之oracle 在分组内排序的方法回顾
- IT忍者神龟之oracle高级CRUD
- IT忍者神龟之windows7安装oracle 10g安装过程及注意事项
- IT忍者神龟之sql回顾Oracle 表连接方式(内连接/外连接/自连接) 详解
- IT忍者神龟之oracle 中一个用户怎么可以不使用用户名访问其他用户的表
- IT忍者神龟之oracle 集合的使用
- IT忍者神龟之oracle中斜杠(/)的含义吐血
- IT忍者神龟之oracle常用日期计算
- IT忍者神龟之Oracle 中 decode 函数用法
- IT忍者神龟之关于oracle with as用法终结
- IT忍者神龟之Oracle DBA经常使用查询吐血列举
- IT忍者神龟之oracle系统包——dbms_job用法(oracle定时任务)别用etl了
- IT该忍者神龟Oracle 树操作(select…start with…connect by…prior)
- IT忍者神龟之Oracle 数据库创建成功之后修改ip地址引发问题