rank() over, dense_rank() over, row_number() over的区别
--ROW_NUMBER() OVER 不需要考虑并列,即使查询出来的数值相同也会进行连续排名
SELECT NAME, STUNO, SUBJECT, SCORE, ROW_NUMBER() OVER(PARTITION BY SUBJECT ORDER BY SCORE DESC) TO_RANK FROM SCOTT.STUDENTINFO;
--RANK() OVER 查出条件后进行排名,成绩相同的两名是并列,下一位同学空出所占的名次
SELECT NAME, STUNO, SUBJECT, SCORE, RANK() OVER(PARTITION BY SUBJECT ORDER BY SCORE DESC) TO_RANK FROM SCOTT.STUDENTINFO;
--DENSE_RANK() OVER 查出条件后进行排名,成绩相同的两名是并列,下一位同学不空出所占的名次
SELECT NAME, STUNO, SUBJECT, SCORE, DENSE_RANK() OVER(PARTITION BY SUBJECT ORDER BY SCORE DESC) TO_RANK FROM SCOTT.STUDENTINFO;
--使用rank() over的时候,空值是最大的,如果排序字段为null,可能造成null字段排在最前面,影响排序结果
SELECT NAME, STUNO, SUBJECT, SCORE, RANK() OVER(PARTITION BY SUBJECT ORDER BY SCORE DESC NULLS LAST) TO_RANK FROM SCOTT.STUDENTINFO;