What#39;s the difference between RANK() and DENSE_RANK() functions in oracle?(oracle 中的 RANK() 和 DENSE_RANK() 函数有什么区别?)
问题描述
RANK()
和 DENSE_RANK()
函数有什么区别?如何在下面的emptbl
表中找出第n个薪水?
What's the difference between RANK()
and DENSE_RANK()
functions? How to find out nth salary in the following emptbl
table?
DEPTNO EMPNAME SAL
------------------------------
10 rrr 10000.00
11 nnn 20000.00
11 mmm 5000.00
12 kkk 30000.00
10 fff 40000.00
10 ddd 40000.00
10 bbb 50000.00
10 ccc 50000.00
如果表数据中有nulls
,如果我想找出nth
的salary怎么办?
If in the table data having nulls
, what will happen if I want to find out nth
salary?
推荐答案
RANK 为您提供有序分区内的排名.平局被分配相同的排名,跳过下一个排名.因此,如果您有 3 个项目处于第 2 位,则列出的下一个排名将是第 5 位.
RANK gives you the ranking within your ordered partition. Ties are assigned the same rank, with the next ranking(s) skipped. So, if you have 3 items at rank 2, the next rank listed would be ranked 5.
DENSE_RANK 再次为您提供有序分区内的排名,但排名是连续的.如果有多个项目的等级,则不跳过等级.
DENSE_RANK again gives you the ranking within your ordered partition, but the ranks are consecutive. No ranks are skipped if there are ranks with multiple items.
至于空值,它取决于 ORDER BY 子句.这是一个简单的测试脚本,您可以使用它来查看会发生什么:
As for nulls, it depends on the ORDER BY clause. Here is a simple test script you can play with to see what happens:
with q as (
select 10 deptno, 'rrr' empname, 10000.00 sal from dual union all
select 11, 'nnn', 20000.00 from dual union all
select 11, 'mmm', 5000.00 from dual union all
select 12, 'kkk', 30000 from dual union all
select 10, 'fff', 40000 from dual union all
select 10, 'ddd', 40000 from dual union all
select 10, 'bbb', 50000 from dual union all
select 10, 'xxx', null from dual union all
select 10, 'ccc', 50000 from dual)
select empname, deptno, sal
, rank() over (partition by deptno order by sal nulls first) r
, dense_rank() over (partition by deptno order by sal nulls first) dr1
, dense_rank() over (partition by deptno order by sal nulls last) dr2
from q;
EMP DEPTNO SAL R DR1 DR2
--- ---------- ---------- ---------- ---------- ----------
xxx 10 1 1 4
rrr 10 10000 2 2 1
fff 10 40000 3 3 2
ddd 10 40000 3 3 2
ccc 10 50000 5 4 3
bbb 10 50000 5 4 3
mmm 11 5000 1 1 1
nnn 11 20000 2 2 2
kkk 12 30000 1 1 1
9 rows selected.
这是一个链接 一个很好的解释和一些例子.
Here's a link to a good explanation and some examples.
这篇关于oracle 中的 RANK() 和 DENSE_RANK() 函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:oracle 中的 RANK() 和 DENSE_RANK() 函数有什么区别?


基础教程推荐
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01