Oracle排名函数rank dense_rank实例详解
(2017-05-25 16:56:23)
标签:
oracle排名函数rankoracle排名函数详解排名函数rank用法示例dense_rank用法详解oracle排名函数示例 |
分类: Oracle数据库 |
在Oracle中,目前已知的排名函数如下:
1、rank 函数,并列时排名相同,占用名次,不连续,空值最大;
2、dense_rank 函数,并列时排名相同,不占用名次,连续,空值最大;
3、row_number 函数,分区内名次连续,空值最大;
函数语法:
rank() over(order by 排序字段 顺序)
rank() over(partition by 分组字段 order by 排序字段 顺序)
dense_rank() over(order by 排序字段 顺序)
dense_rank() over(partition by 分组字段 order by 排序字段 顺序)
row_number() over(order by 排序字段 顺序)
row_number() over(partition by 分组字段 order by 排序字段 顺序)
一、创建测试表,添加测试数据
drop table my_test;
create table my_test
(
);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (8, 1, '张三 ', '语文', 80);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (9, 2, '李四 ', '数学', 80);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (10, 1, '张三 ', '数学', 0);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (11, 2, '李四 ', '语文', 50);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (12, 3, '王五 ', '语文', 10);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (13, 3, '王五 ', '数学', null);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (14, 3, '王五 ', '体育', 120);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (15, 4, '王二 ', ';
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (16, 5, '麻子 ', 'c++', 80);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (3, 3, '王五 ', 'oracle', 0);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (4, 4, '王二 ', 'oracle', 77);
insert into my_test (autoid, stu_id, stu_name, sub_name, score)
values (17, 2, '李四 ', 'oracle', 77);
commit;
二、实例
1、查询原始数据:学号,姓名,科目名称,成绩
SQL> select * from my_test;
---------- ------ -------- -------------------- ------------