有如下表
1.row_number
row_number在排名时序号 连续 不重复,即使遇到表中的两个3时亦如此
执行
select row_number() OVER(order by e.salary desc) as row_num , e.salary
from employee e
结果如图
注意:在使用row_number实现分页时需要特别注意一点,over子句中的order by 要与Sql排序记录中的order by 保持一致,否则得到的序号可能不是连续的
例如:
执行
select row_number() OVER(order by e.salary desc) as row_num , e.salary
from employee e
order by e.time_length
结果
2.rank
rank函数会把要求排序的值相同的归为一组且每组序号一样,排序不会连续
执行
select rank() OVER(order by e.salary desc) as row_num , e.salary
from employee e
结果:
3.dense_rank
dense_rank排序是连续的,也会把相同的值分为一组且每组排序号一样
执行
select dense_rank() OVER(order by e.salary desc) as row_num , e.salary
from employee e
结果:
4.NTILE
NTILE(group_num)将所有记录分成group_num个组,每组序号一样
执行
select NTILE(2) OVER(order by e.salary desc) as row_num , e.salary
from employee e
结果: