--已知:两种排名方式(分区和不分区):使用和不使用partition
--两种计算方式(连续,不连续),对应函数:dense_rank,rank
·查询原始数据:学号,姓名,科目名,成绩
select
*
from
t_
score
ottom" width="51">
S_ID
ottom" width="118">
S_NAME
ottom" width="120">
SUB_NAME
ottom" width="108">
ottom" width="51">
1
ottom" width="118">
张三
ottom" width="120">
语文
ottom" width="108">
80.00
ottom" width="51">
2
ottom" width="118">
李四
ottom" width="120">
数学
ottom" width="108">
80.00
ottom" width="51">
1
ottom" width="118">
张三
ottom" width="120">
数学
ottom" width="108">
0.00
ottom" width="51">
2
ottom" width="118">
李四
ottom" width="120">
语文
ottom" width="108">
50.00
ottom" width="51">
3
ottom" width="118">
张三丰
ottom" width="120">
语文
ottom" width="108">
10.00
ottom" width="51">
3
ottom" width="118">
张三丰
ottom" width="120">
数学
ottom" width="108">
ottom" width="51">
3
ottom" width="118">
张三丰
ottom" width="120">
体育
ottom" width="108">
120.00
ottom" width="51">
4
ottom" width="118">
杨过
ottom" width="120">
JAVA
ottom" width="108">
90.00
ottom" width="51">
5
ottom" width="118">
mike
ottom" width="120">
c++
ottom" width="108">
80.00
ottom" width="51">
3
ottom" width="118">
张三丰
ottom" width="120">
Oracle
ottom" width="108">
0.00
ottom" width="51">
4
ottom" width="118">
杨过
ottom" width="120">
Oracle
ottom" width="108">
77.00
ottom" width="51">
2
ottom" width="118">
李四
ottom" width="120">
Oracle
ottom" width="108">
77.00
select
sc.s_id,sc.s_name,sub_name,sc.
score,
rank() over
(
order
by
score desc
) 名次
where
sub_name='Oracle'
ottom" width="49">
S_ID
ottom" width="108">
S_NAME
ottom" width="108">
SUB_NAME
ottom" width="84">
ottom" width="60">
名次
ottom" width="49">
4
ottom" width="108">
杨过
ottom" width="108">
Oracle
ottom" width="84">
77.00
ottom" width="60">
1
ottom" width="49">
2
ottom" width="108">
李四
ottom" width="108">
Oracle
ottom" width="84">
77.00
ottom" width="60">
1
ottom" width="49">
3
ottom" width="108">
张三丰
ottom" width="108">
Oracle
ottom" width="84">
0.00
ottom" width="60">
3
对比:rank()与dense_rank():非连续排名与连续排名(都是简单排名)
select
sc.s_id,
dense_rank() over
(
order
by
score desc
) 名次
where
sub_name='Oracle'
ottom" width="49">
S_ID
ottom" width="96">
S_NAME
ottom" width="96">
SUB_NAME
ottom" width="84">
ottom" width="60">
名次
ottom" width="49">
4
ottom" width="96">
杨过
ottom" width="96">
Oracle
ottom" width="84">
77.00
ottom" width="60">
1
ottom" width="49">
2
ottom" width="96">
李四
ottom" width="96">
Oracle
ottom" width="84">
77.00
ottom" width="60">
1
ottom" width="49">
3
ottom" width="96">
张三丰
ottom" width="96">
Oracle
ottom" width="84">
0.00
ottom" width="60">
2
select
sc.s_id,
rank() over
(
partition
by
sub_name
order
by
score desc
) 名次
ottom" width="64">
S_ID
ottom" width="81">
S_NAME
ottom" width="96">
SUB_NAME
ottom" width="84">
ottom" width="72">
名次
ottom" width="64">
4
ottom" width="81">
杨过
ottom" width="96">
JAVA
ottom" width="84">
90.00
ottom" width="72">
1
ottom" width="64">
4
ottom" width="81">
杨过
ottom" width="96">
Oracle
ottom" width="84">
77.00
ottom" width="72">
1
ottom" width="64">
2
ottom" width="81">
李四
ottom" width="96">
Oracle
ottom" width="84">
77.00
ottom" width="72">
1
ottom" width="64">
3
ottom" width="81">
张三丰
ottom" width="96">
Oracle
ottom" width="84">
0.00
ottom" width="72">
3
ottom" width="64">
5
ottom" width="81">
mike
ottom" width="96">
c++
ottom" width="84">
80.00
ottom" width="72">
1
ottom" width="64">
3
ottom" width="81">
张三丰
ottom" width="96">
数学
ottom" width="84">
ottom" width="72">
1
ottom" width="64">
2
ottom" width="81">
李四
ottom" width="96">
数学
ottom" width="84">
80.00
ottom" width="72">
2
ottom" width="64">
1
ottom" width="81">
张三
ottom" width="96">
数学
ottom" width="84">
0.00
ottom" width="72">
3
ottom" width="64">
3
ottom" width="81">
张三丰
ottom" width="96">
体育
ottom" width="84">
120.00
ottom" width="72">
1
ottom" width="64">
1
ottom" width="81">
张三
ottom" width="96">
语文
ottom" width="84">
80.00
ottom" width="72">
1
ottom" width="64">
2
ottom" width="81">
李四
ottom" width="96">
语文
ottom" width="84">
50.00
ottom" width="72">
2
ottom" width="64">
3
ottom" width="81">
张三丰
ottom" width="96">
语文
ottom" width="84">
10.00
ottom" width="72">
3
·查询各科前2名(分区排名)
·类似:新闻表,求栏目点击率在前3位的新闻。
商品表,求各类别销售额在前10位的商品。
rush:
sql;">
select * from (
select sc.s_id,dense_rank() over
(partition by sub_name order by
score desc) 名次
from t_
score sc
) x
where x.名次<=2
ottom" width="67">
S_ID
ottom" width="99">
S_NAME
ottom" width="111">
SUB_NAME
ottom" width="77">
ottom" width="64">
名次
ottom" width="67">
ottom" width="99">
ottom" width="111">
ottom" width="77">
ottom" width="64">
ottom" width="67">
ottom" width="99">
ottom" width="111">
ottom" width="77">
ottom" width="64">
ottom" width="67">
ottom" width="99">
ottom" width="111">
ottom" width="77">
ottom" width="64">
ottom" width="67">
ottom" width="99">
ottom" width="111">
ottom" width="77">
ottom" width="64">
ottom" width="67">
ottom" width="99">
ottom" width="111">
ottom" width="77">
ottom" width="64">
ottom" width="67">
ottom" width="99">
ottom" width="111">
ottom" width="77">
ottom" width="64">
ottom" width="67">
ottom" width="99">
ottom" width="111">
ottom" width="77">
ottom" width="64">
ottom" width="67">
ottom" width="99">
ottom" width="111">
ottom" width="77">
ottom" width="64">
ottom" width="67">
ottom" width="99">
ottom" width="111">
ottom" width="77">
ottom" width="64">
ottom" width="67">
ottom" width="99">
ottom" width="111">
group
by
s_id,s_name
ottom" width="67">
S_ID
ottom" width="99">
S_NAME
ottom" width="111">
ottom" width="67">
1
ottom" width="99">
张三
ottom" width="111">
80.00
ottom" width="67">
2
ottom" width="99">
李四
ottom" width="111">
207.00
ottom" width="67">
3
ottom" width="99">
张三丰
ottom" width="111">
130.00
ottom" width="67">
4
ottom" width="99">
杨过
ottom" width="111">
167.00
ottom" width="67">
5
ottom" width="99">
mike
ottom" width="111">
80.00
select
x.*,
rank() over (
order
by
sum_
score desc
) 名次
from
(
select
s_id,s_name ) x
ottom" width="67">
S_ID
ottom" width="99">
S_NAME
ottom" width="111">
ottom" width="77">
名次
ottom" width="67">
2
ottom" width="99">
李四
ottom" width="111">
207.00
ottom" width="77">
1
ottom" width="67">
4
ottom" width="99">
杨过
ottom" width="111">
167.00
ottom" width="77">
2
ottom" width="67">
3
ottom" width="99">
张三丰
ottom" width="111">
130.00
ottom" width="77">
3
ottom" width="67">
1
ottom" width="99">
张三
ottom" width="111">
80.00
ottom" width="77">
4
ottom" width="67">
5
ottom" width="99">
mike
ottom" width="111">
80.00
ottom" width="77">
4
语法:
rank() over (
order
by
排序字段 顺序)
rank() over (
partition
by
分组字段 order
by
排序字段 顺序)
1.顺序:asc|desc
名次与业务相关:
2.分区字段:根据什么字段进行分区。
问题:分区与分组有什么区别?
·分区只是将原始数据进行名次排列(记录数不变),
·分组是对原始数据进行聚合
统计(记录数变少,每组返回一条),注意:聚合。
脚本:
rush:
sql;">
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,
score)
values (8,1,'张三 ','语文',80);
insert into t_
score (autoid,
score)
values (9,2,'李四 ','数学',
score)
values (10,0);
insert into t_
score (autoid,
score)
values (11,50);
insert into t_
score (autoid,
score)
values (12,3,'张三丰 ',10);
insert into t_
score (autoid,
score)
values (13,null);
insert into t_
score (autoid,
score)
values (14,'体育',120);
insert into t_
score (autoid,
score)
values (15,4,'杨过 ','java',90);
insert into t_
score (autoid,
score)
values (16,5,'mike ','c++',
score)
values (3,'oracle',
score)
values (4,77);
insert into t_
score (autoid,
score)
values (17,77);
commit;