SQL三种排序(开窗)第几名/前几名/topN
- 1三种排序(开窗)第几名/前几名/topN
- 思路
- 4种排序开窗函数
1三种排序(开窗)第几名/前几名/topN
求每个学生成绩第二高的科目-排序
思路
t2表:对每个学生 的所有课程 的成绩排序,定好新列rn;(dense_rank() over (),)(partition by student order by socre)(开窗函数:基于每个student 对 score排序)
select class,student,score,dense_rank() over (
partition by student order by score DESC
) as rn
from day_sql.day2_kaichaunghanshu
t3表:取a1表中rn=2的数据。
select class,student,score
from (
select class,student,score,dense_rank() over (
partition by student order by score
) as rn
from day_sql.day2_kaichaunghanshu
)t2 where rn=2;
4种排序开窗函数
3种序号排序
rank() -12245
dense_rank() -12234
row_number() -12345
1种百分比排序
percent_rank() - 百分比超过了多少人