问题描述
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
薪水会怎样?
If in the table data having nulls
, what will happen if I want to find out nth
salary?
推荐答案
RANK为您提供有序分区内的排名.领带被分配相同的等级,而下一个等级被跳过.因此,如果您在第2级有3个项目,则列出的下一个级别将是第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()函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!