---
create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int);
insert into Fruit values(1,'苹果',1000,2000,3300,5000);
insert into Fruit values(2,'橘子',3000,3000,3200,1500);
insert into Fruit values(3,'香蕉',2500,3500,2200,2500);
insert into Fruit values(4,'葡萄',1500,2500,1200,3500);
/*
unpivot 行转列
顾名思义就是将多列转换成1列中去
案例:现在有一个水果表,记录了4个季度的销售数量,现在要将每种水果的每个季度的销售情况用多行数据展示。
*/
select * from Fruit;
select id , name, jidu, xiaoshou from Fruit unpivot (xiaoshou for jidu in (q1, q2, q3, q4));
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
pivot 列转行
测试数据 (id,类型名称,销售数量),案例:根据水果的类型查询出一条数据显示出每种类型的销售数量。
*/
create table demo(id int,name varchar(20),nums int); ---- 创建表
insert into demo values(1, '苹果', 1000);
insert into demo values(2, '苹果', 2000);
insert into demo values(3, '苹果', 4000);
insert into demo values(4, '橘子', 5000);
insert into demo values(5, '橘子', 3000);
insert into demo values(6, '葡萄', 3500);
insert into demo values(7, '芒果', 4200);
insert into demo values(8, '芒果', 5500);
select * from demo;
--分组查询
select name, sum(nums) nums from demo group by name;
--行转列查询
select * from (select name, nums from demo) pivot (sum(nums) for name in ('苹果' 苹果, '橘子', '葡萄', '芒果'));
--当然也可以不使用pivot函数,等同于下列语句,只是代码比较长,容易理解
select *
from (select sum(nums) 苹果 from demo where name = '苹果'),
(select sum(nums) 橘子 from demo where name = '橘子'),
(select sum(nums) 葡萄 from demo where name = '葡萄'),
(select sum(nums) 芒果 from demo where name = '芒果');
create table t(
stuid varchar2(20),
course varchar2(20),
score number(8,2));
insert into t values('1','math',80);
insert into t values('1','chinese',90);
insert into t values('1','english',85);
insert into t values('1','history',78);
insert into t values('2','math',82);
insert into t values('2','chinese',86);
insert into t values('2','english',90);
create table t(
stuid varchar2(20),
course varchar2(20),
score number(8,2));
insert into t values('1','math',80);
insert into t values('1','chinese',90);
insert into t values('1','english',85);
insert into t values('1','history',78);
insert into t values('2','math',82);
insert into t values('2','chinese',86);
insert into t values('2','english',90);
15:51:49 BZJ@itps>select * from t;
STUID COURSE SCORE
-------------------- -------------------- ----------
1 math 80
1 chinese 90
1 english 85
1 history 78
2 math 82
2 chinese 86
2 english 90
2 history 88
8 rows selected.
比如t表记录每名学生本学年所有考试的成绩(course列是学科,改为考试名称),那么我们想查询到每名学生考试的前三得分,并用一列表示,则
16:07:12 BZJ@itps>select stuid,max(decode(rn,1,score,0)) score_1,
16:07:27 2 max(decode(rn,2,score,0)) score_2,
16:07:33 3 max(decode(rn,3,score,0)) score_3
16:07:36 4 from (select stuid,score,row_number() over(partition by stuid order by score desc) rn from t) -------------此处可以采用(rank() over,dense_rank(),row_number())其中之一,三个函数有区别,在此不作说明。
16:07:48 5 group by stuid;
STUID SCORE_1 SCORE_2 SCORE_3
-------------------- ---------- ---------- ----------
1 90 85 80
2 90 88 86
现在回归到表t记录学生一次考试每门学科的成绩,我们想要用一列展示一名学生的所有学科的成绩,则
方法一:
16:13:31 BZJ@itps>select stuid,
16:13:37 2 max(decode(course,'math',score,0)) math,
16:13:54 3 max(decode(course,'chinese',score,0)) chinese,
16:14:08 4 max(decode(course,'english',score,0)) english,
16:14:24 5 max(decode(course,'history',score,0)) history
16:14:37 6 from t group by stuid;
STUID MATH CHINESE ENGLISH HISTORY
-------------------- ---------- ---------- ---------- ----------
1 80 90 85 78
2 82 86 90 88
Elapsed: 00:00:00.01
方法二:
16:14:46 BZJ@itps>select stuid,
16:18:29 2 max(case course when 'math' then score else 0 end) math,
16:19:48 3 max(case course when 'chinese' then score else 0 end) chinese,
16:20:18 4 max(case course when 'english' then score else 0 end) english,
16:20:48 5 max(case course when 'history' then score else 0 end) history
16:21:17 6 from t group by stuid;
STUID MATH CHINESE ENGLISH HISTORY
-------------------- ---------- ---------- ---------- ----------
1 80 90 85 78
2 82 86 90 88
Elapsed: 00:00:00.00
我们也可以直接得出改学生本次考试总分,则
16:53:30 BZJ@itps>select stuid,
16:53:48 2 max(decode(course,'math',score,0)) math,
16:53:51 3 max(decode(course,'chinese',score,0)) chinese,
16:53:54 4 max(decode(course,'english',score,0)) english,
16:53:56 5 max(decode(course,'history',score,0)) history,
16:54:00 6 sum(score) total
16:54:09 7 from t group by stuid;
STUID MATH CHINESE ENGLISH HISTORY TOTAL
-------------------- ---------- ---------- ---------- ---------- ----------
1 80 90 85 78 333
2 82 86 90 88 346
Elapsed: 00:00:00.00