赞
踩
RANK():计算带有重复值的排名,相同值将会得到相同的排名,并且在有相同排名的情况下,下一个排名将跳过相应数量的排名。
SELECT
employee_id,
name,
department,
sales_amount,
RANK() OVER (PARTITION BY department ORDER BY sales_amount DESC) AS sales_rank
FROM employees;
注:rank()函数只要遇到重复的数据排名就会放在最后
单重复: 单重复就放在最后
employee_id | name | department | sales_amount | sales_rank |
---|---|---|---|---|
1 | Alice | Sales | 5000 | 2 |
3 | Charlie | Sales | 5500 | 1 |
5 | Eve | Sales | 4500 | 3 |
6 | James | Marketing | 6500 | 1 |
7 | Jack | Marketing | 5500 | 2 |
2 | Bob | Marketing | 6000 | 3 |
4 | David | Marketing | 6000 | 3 |
**多重复:**有多重复的结果会进行重复结果排序
Alice | Sales | 5000 | 2 | |
3 | Charlie | Sales | 5500 | 1 |
5 | Eve | Sales | 4500 | 3 |
6 | James | Marketing | 6500 | 1 |
2 | Bob | Marketing | 6000 | 2 |
4 | David | Marketing | 6000 | 2 |
8 | Jam | Marketing | 5500 | 4 |
7 | Jack | Marketing | 5500 | 4 |
RANK()
,但是不会跳过相同排名的行,而是会依次分配紧邻的排名。SELECT
student_id,
subject,
score,
DENSE_RANK() OVER (PARTITION BY subject ORDER BY score DESC) AS dense_rank
FROM scores;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。