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 rnfrom 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 rnfrom day_sql.day2_kaichaunghanshu)t2 where rn=2;
4種排序開窗函數
3種序號排序
rank() -12245
dense_rank() -12234
row_number() -12345
1種百分比排序
percent_rank() - 百分比超過了多少人