Oracle函数Rank Over Partition使用实例详解(一)

Rank()使用说明:

 

a. 函数简介:

    返回结果集分区内指定字段的值的排名,指定字段的值的排名是相关行之前的排名加一。

b. 语法:

    RANK() OVER([<partiton_by_clause>]<order by clause>)

c. 参数说明:

    partition_by_clause 将from子句生成的结果集划分为应用到RANK函数的分区。

    Order_by_clause确定将RANK值应用到分区中的行时所使用的顺序。

 

d. 以下是实例使用:

 

 

1. 创建测试表

 

--创建表
-- Create table
create table T_SCORE
(
  AUTOID   NUMBER not null,
  S_ID     NUMBER(3),
  S_NAME   CHAR(8) not null,
  SUB_NAME VARCHAR2(20),
  SCORE    NUMBER(10,2)
);
-- Add comments to the table 
comment on table T_SCORE
  is '学生成绩表';
-- Add comments to the columns 
comment on column T_SCORE.AUTOID
  is '主键ID';
comment on column T_SCORE.S_ID
  is '学生ID';
comment on column T_SCORE.S_NAME
  is '学生姓名';
comment on column T_SCORE.SUB_NAME
  is '科目';
comment on column T_SCORE.SCORE
  is '成绩';

 

2. 创建测试记录

 

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (8, 1, '张三    ', '语文', 80.00);

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (9, 2, '李四    ', '数学', 80.00);

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (10, 1, '张三    ', '数学', 0.00);

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (11, 2, '李四    ', '语文', 50.00);

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (12, 3, '张三丰  ', '语文', 10.00);

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.00);

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (15, 4, '杨过    ', 'JAVA', 90.00);

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (16, 5, 'mike    ', 'c++', 80.00);

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (3, 3, '张三丰  ', 'Oracle', 0.00);

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (4, 4, '杨过    ', 'Oracle', 77.00);

insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)
values (17, 2, '李四    ', 'Oracle', 77.00);

 

3. 分不同情况查询

3.1 查询所有的学生成绩

 

--1.查询所有的学生成绩
select t.s_id 学号, t.s_name 姓名, t.sub_name 科目, t.score 成绩
  from t_score t;

查询结果:

学号姓名科目成绩
1张三     语文80.00
2李四     数学80.00
1张三     数学0.00
2李四     语文50.00
3张三丰   语文10.00
3张三丰   数学
3张三丰   体育120.00
4杨过     JAVA90.00
5mike     c++80.00
3张三丰   Oracle0.00
4杨过     Oracle77.00
2李四     Oracle77.00

 

3.2 查询Oracle科目成绩名次-非连续rank

--2.查询Oracle科目成绩名次-非连续rank
select t.s_id 学号,
       t.s_name 姓名,
       t.sub_name 科目,
       t.score 成绩,
       rank() over(order by score desc nulls last) 名次
  from t_score t
 where t.sub_name = 'Oracle';

 查询结果:

学号姓名科目成绩名次
4杨过     Oracle77.001
2李四     Oracle77.001
3张三丰   Oracle0.003

 

3.3查询Oracle科目成绩名次-连续dense_rank

--3.查询Oracle科目成绩名次-连续dense_rank
select t.s_id 学号,
       t.s_name 姓名,
       t.sub_name 科目,
       t.score 成绩,
       dense_rank() over(order by score desc nulls last) 名次
  from t_score t
 where t.sub_name = 'Oracle';

 查询结果:

 

学号姓名科目成绩名次
4杨过     Oracle77.001
2李四     Oracle77.001
3张三丰   Oracle0.002

 

3.4 查询各学生各科排名

--4.查询各学生各科排名
select t.s_id 学号,
       t.s_name 姓名,
       t.sub_name 科目,
       t.score 成绩,
       dense_rank() over(partition by t.s_name order by score desc nulls last) 名次
  from t_score t;

 查询结果:

 

学号姓名科目成绩名次
5mike     c++80.001
2李四     数学80.001
2李四     Oracle77.002
2李四     语文50.003
4杨过     JAVA90.001
4杨过     Oracle77.002
1张三     语文80.001
1张三     数学0.002
3张三丰   体育120.001
3张三丰   语文10.002
3张三丰   Oracle0.003
3张三丰   数学
4

 

3.5 查询各科名次(分区)

--5.查询各科名次(分区)
select t.s_id 学号,
       t.s_name 姓名,
       t.sub_name 科目,
       t.score 成绩,
       dense_rank() over(partition by t.sub_name order by score desc nulls last) 名次
  from t_score t;

 查询结果:

 

学号姓名科目成绩名次
4杨过     JAVA90.001
4杨过     Oracle77.001
2李四     Oracle77.001
3张三丰   Oracle0.002
5mike     c++80.001
2李四     数学80.001
1张三     数学0.002
3张三丰   数学
3
3张三丰   体育120.001
1张三     语文80.001
2李四     语文50.002
3张三丰   语文10.003

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值