Oracle 分析函数的汇总
Posted 大树的困惑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle 分析函数的汇总相关的知识,希望对你有一定的参考价值。
Oracle 分析函数的汇总
这里主要整理over()的分析函数组合
测试数据
create table students
(id number(15,0),
area varchar2(10),
stu_type varchar2(2),
score number(20,2));
insert into students values(1, '111', 'g', 80 );
insert into students values(1, '111', 'j', 80 );
insert into students values(1, '222', 'g', 89 );
insert into students values(1, '222', 'g', 68 );
insert into students values(2, '111', 'g', 80 );
insert into students values(2, '111', 'j', 70 );
insert into students values(2, '222', 'g', 60 );
insert into students values(2, '222', 'j', 65 );
insert into students values(3, '111', 'g', 75 );
insert into students values(3, '111', 'j', 58 );
insert into students values(3, '222', 'g', 58 );
insert into students values(3, '222', 'j', 90 );
insert into students values(4, '111', 'g', 89 );
insert into students values(4, '111', 'j', 90 );
insert into students values(4, '222', 'g', 90 );
insert into students values(4, '222', 'j', 89 );
commit;
1.DENSE_RANK() over()
允许并列名次、名次不间断,DENSE_RANK(),结果如122344456……
--将score按ID分组排名
dense_rank() over(partition by id order by score desc)
--将score不分组排名:
dense_rank() over(order by score desc)
--测试
select id,area,score,
dense_rank() over(partition by id order by score desc) rn1 ,--分组id排序,
dense_rank() over(order by score desc) rn2--不分组排序
from students order by id,area;
2.ROW_NUMBER() over()
不允许并列名次、相同值名次不重复,ROW_NUMBER(),结果如123456……
--将score按ID分组排名:
row_number() over(partition by id order by score desc)
--将score不分组排名:
row_number() over(order by score desc)
--测试
select id,area,score,
row_number() over(partition by id order by score desc) rn1 ,--分组id排序,
row_number() over(order by score desc) rn2 --不分组排序
from students order by id,area;
3.rank() over()
允许并列名次、复制名次自动空缺,rank(),结果如12245558……
--将score按ID分组排名:
rank() over(partition by id order by score desc)
--将score不分组排名:
rank() over(order by score desc)
--测试
select id,area,score,
rank() over(partition by id order by score desc) rn1, --分组id排序,
rank() over(order by score desc) rn2 --不分组排序
from students order by id,area;
4.cume_dist() over()
名次分析,cume_dist()——-最大排名/总个数
用来分析目标在对应整体水平中的占比
如a根据id进行正序排列,所以id小的占比小,id大的占比大(体现了当前值在整体中的水平)
--函数:
cume_dist() over(order by id)
--测试
select id,area,score,
cume_dist() over(order by id) a, --按ID最大排名/总个数
cume_dist() over(partition by id order by score desc) b, --ID分组中,scroe最大排名值/本组总个数
row_number() over (order by id) --记录号
from students order by id,area;
5.分组统计-sum() over()
select id,area,score,
sum(1) over() as 总记录数,
sum(1) over(partition by id) as 分组记录数,
sum(score) over() as 总计 ,
sum(score) over(partition by id) as 分组求和,
sum(score) over(order by id) as 分组连续求和,
sum(score) over(partition by id,area) as 分组ID和area求和,
sum(score) over(partition by id order by area) as 分组ID并连续按area求和
from students;
6.分组统计-max() over()
select id,area,score,
max(score) over() as 最大值,
max(score) over(partition by id) as 分组最大值,
max(score) over(order by id) as 分组连续最大值,
max(score) over(partition by id,area) as 分组ID和area求最大值,
max(score) over(partition by id order by area) as 分组ID并连续按area求最大值
from students;
7.分组统计-avg() over()
select id,area,score,
avg(score) over() as 所有平均,
avg(score) over(partition by id) as 分组平均,
avg(score) over(order by id) as 分组连续平均,
avg(score) over(partition by id,area) as 分组ID和area平均,
avg(score) over(partition by id order by area) as 分组ID并连续按area平均
from students;
8.分组统计-RATIO_TO_REPORT() over()
类似于 score/sum(score)
select id,area,score,
RATIO_TO_REPORT(score) over() as "占所有%",
RATIO_TO_REPORT(score) over(partition by id) as "占分组%",
from students;
9.LAG(COL,n,default) over()
--取前后边N条数据
--取前面记录的值:lag(score,n,x) over(order by id) 取当前行前面第N条数据的score字段,如果为空,取x
select id,
lag(score,1,0) over(order by id) lg,
score
from students;
10.LEAD(COL,n,default) over()
--取后边N条数据
--取后面记录的值:lead(score,n,x) over(order by id) 取当前行后面第N条数据的score字段,如果为空,取x
select id,
lead(score,1,0) over(order by id) lg,
score
from students;
11.FIRST_VALUE() over()
--取第起始1行值:first_value(score,n) over(order by id)
select id,first_value(score) over(order by id) fv,score from students;
12.LAST_VALUE() over()
--取第最后1行值:LAST_value(score,n) over(order by id)
select id,last_value(score) over(order by id) lv,score from students;
以上是关于Oracle 分析函数的汇总的主要内容,如果未能解决你的问题,请参考以下文章
分析函数系列之sum(col1) over(partition by col2 order by col3):实现分组汇总或递增汇总