这篇文章主要介绍了Oracle排名函数(Rank)实例详解,需要的朋友可以参考下
 
 

--已知:两种排名方式(分区和不分区):使用和不使用partition

--两种计算方式(连续,不连续),对应函数:dense_rank,rank

·查询原始数据:学号,姓名,科目名,成绩

select * from t_score

S_IDS_NAMESUB_NAMESCORE
1张三语文80.00
2李四数学80.00
1张三数学0.00
2李四语文50.00
3张三丰语文10.00
3张三丰数学 
3张三丰体育120.00
4杨过JAVA90.00
5mikec++80.00
3张三丰Oracle0.00
4杨过Oracle77.00
2李四Oracle77.00
 
·查询各学生科目为Oracle排名(简单排名)
select sc.s_id,sc.s_name,sub_name,sc.score,
rank() over (order by score desc) 名次
from t_score sc
where sub_name='Oracle'
 
S_IDS_NAMESUB_NAMESCORE名次
4杨过Oracle77.001
2李四Oracle77.001
3张三丰Oracle0.003
 
 
对比:rank()与dense_rank():非连续排名与连续排名(都是简单排名)
 
select sc.s_id,sc.s_name,sub_name,sc.score,
dense_rank() over (order by score desc) 名次
from t_score sc
where sub_name='Oracle'
S_IDS_NAMESUB_NAMESCORE名次
4杨过Oracle77.001
2李四Oracle77.001
3张三丰Oracle0.002
 
·查询各学生各科排名(分区排名)
select sc.s_id,sc.s_name,sub_name,sc.score,
rank() over
(partition by sub_name order by score desc) 名次
from t_score sc
S_IDS_NAMESUB_NAMESCORE名次
4杨过JAVA90.001
4杨过Oracle77.001
2李四Oracle77.001
3张三丰Oracle0.003
5mikec++80.001
3张三丰数学 1
2李四数学80.002
1张三数学0.003
3张三丰体育120.001
1张三语文80.001
2李四语文50.002
3张三丰语文10.003

·查询各科前2名(分区排名)

·类似:新闻表,求栏目点击率在前3位的新闻。
商品表,求各类别销售额在前10位的商品。

1
2
3
4
5
6
7
select * from (
select sc.s_id,sc.s_name,sub_name,sc.score,
dense_rank() over
(partition by sub_name order by score desc) 名次
from t_score sc
) x
where x.名次<=2
S_ID
S_NAME
SUB_NAME
SCORE
名次
4
杨过
JAVA
90.00
1
4
杨过
Oracle
77.00
1
2
李四
Oracle
77.00
1
3
张三丰
Oracle
0.00
2
5
mike
c++
80.00
1
3
张三丰
数学
 
1
2
李四
数学
80.00
2
3
张三丰
体育
120.00
1
1
张三
语文
80.00
1
2
李四
语文
50.00
2
 
 
·查询各同学总分
select s_id,s_name,sum(score) sum_score from t_score
group by s_id,s_name
S_IDS_NAMESUM_SCORE
1张三 80.00
2李四 207.00
3张三丰130.00
4杨过 167.00
5mike 80.00
 
·根据总分查询各同学名次
select x.*,
rank() over (order by sum_score desc) 名次
from (
select s_id,s_name,sum(score) sum_score from t_score
group by s_id,s_name ) x
S_IDS_NAMESUM_SCORE名次
2李四 207.001
4杨过 167.002
3张三丰 130.003
1张三 80.004
5mike 80.004
 
 
语法:
rank() over (order by 排序字段 顺序)
rank() over (partition by 分组字段 order by 排序字段 顺序)
 
1.顺序:asc|desc 名次与业务相关:
示例:找求优秀学员:成绩:降序 迟到次数:升序
2.分区字段:根据什么字段进行分区。
 
问题:分区与分组有什么区别?
·分区只是将原始数据进行名次排列(记录数不变),
·分组是对原始数据进行聚合统计(记录数变少,每组返回一条),注意:聚合。

脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
create table t_score
(
autoid number primary key,
s_id number(3),
s_name char(8) not null,
sub_name varchar2(20),
score number(10,2)
);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (8, 1, '张三 ', '语文', 80);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (9, 2, '李四 ', '数学', 80);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (10, 1, '张三 ', '数学', 0);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (11, 2, '李四 ', '语文', 50);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (12, 3, '张三丰 ', '语文', 10);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (13, 3, '张三丰 ', '数学', null);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (14, 3, '张三丰 ', '体育', 120);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (15, 4, '杨过 ', 'java', 90);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (16, 5, 'mike ', 'c++', 80);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (3, 3, '张三丰 ', 'oracle', 0);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (4, 4, '杨过 ', 'oracle', 77);
insert into t_score (autoid, s_id, s_name, sub_name, score)
values (17, 2, '李四 ', 'oracle', 77);
commit;
04-12 17:57