ROW_NUMBER和RANK和DENSE_RANK的区别

Sql格式

SELECT *,
row_number() over(ORDER BY investment desc) as `row_number`,
dense_rank() over(order by investment desc) as `dense_rank`,
rank() over(order by investment desc) as `rank`
FROM investmenttab
  • row_number 排序值从小到大,依次排列

  • dense_rank 相同数据,并列保存,不存在断值(一直连续)

  • rank 相同数据并列保存,下一个值跳值(断续)
    ROW_NUMBER和RANK和DENSE_RANK的区别_第1张图片

  • 分组获取每组前十

select aaa.* 
from 
(
SELECT
area,investment,
rank() over(PARTITION by area order by investment desc) as `rank`
from 
investmenttab tab 
left join users us
on tab.userid = us.userid
) aaa
where aaa.rank <10

ROW_NUMBER和RANK和DENSE_RANK的区别_第2张图片

你可能感兴趣的:(MySQL,数据库开发)