您的位置:首页 > 数据库 > Oracle

Oracle分析函数之Rank()和DENSE_RANK()详解

2015-01-04 14:07 501 查看
Rank()使用说明:

a. 函数简介:

    返回结果集分区内指定字段的值的排名,指定字段的值的排名是相关行之前的排名加一(排名不一定连续,在排名值上留下空隙)。

b. 语法:

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

c. 参数说明:

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

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

Dense_rank()函数:返回一个唯一的值,除非当碰到相同数据时,此时所有相同数据的排名都是一样的(排名是连续,在排名值上不会留下空隙)。

如果结果集中有空值,rank()和 dense_rank()同时可以用nulls first或者nulls last子句控制。

 

d. 以下是实例使用:

 

 

1. 创建测试表

 

Sql代码  


--创建表  

-- 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. 创建测试记录

 

Sql代码  


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 查询所有的学生成绩

 

Sql代码  


--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

Sql代码  


--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

Sql代码  


--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 查询各学生各科排名

Sql代码  


--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 查询各科名次(分区)

Sql代码  


--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
整理自网络

------------------------------------------------------------------------------

Blog: http://blog.csdn.net/tanzuai
IT群:69254049; 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle rank