rank() over,dense_rank() over,row_number() over的区别

rank() over,dense_rank() over,row_number() over的区别

1.rank() over: rank after checking the specified conditions. The characteristic is that if it is to rank students, use this function, if two students with the same score are tied, the next student will vacate the ranking.

select name,subject,score,rank() over(partition by subject order by score desc) rankfrom student_score;

 

 

2. Dense_rank() over: The difference with ran() over is that after the scores of two students are tied, the next student does not vacate the position occupied.

select name,subject,score,dense_rank() over(partition by subject order by score desc) rankfrom student_score;

 

3. Row_number() over this function does not need to consider whether it is tied or not, even if the values ​​queried according to the conditions are the same, it will be ranked continuously

select name,subject,score,row_number() over(partition by subject order by score desc) rankfrom student_score;

 

4. When using rank() over, the null value is the largest. If the sort field is null, it may cause the null field to be ranked first and affect the sort result. You can do this: rank() over(partition by course order by score desc nulls last) to circumvent this problem.

select name,subject,score,rank() over(partition by subject order by score desc nulls last) rankfrom student_score;

Reprinted from: https://www.cnblogs.com/scwbky/p/9558203.html

Guess you like

Origin blog.csdn.net/admin123404/article/details/104949415