分组排序功能(Oracle)
row_number() over()分组排序功能:
-
语法格式:
row_number() over(partition by 分组列 order by 排序列 desc) -
注意:
在使用 row_number() over()函数时候,over()里头的分组以及排序的执行晚于 where 、group by、 order by 的执行。 -
例子:查询 每个科目的前三名
create table t_score(
id int primary key,
s_id int ,
subject varchar(20),
score int ,
class_id int
)
select * from(
select subject,s_id,row_number() over(partition by subject order by score desc) as num
from t_score
) t where T.num <= 3 order by subject
根据成绩确定A,B,C,D四个档次
select case
when (成绩字段>80 and 成绩字段<100) then 'C'
when (成绩字段>60 and 成绩字段<80) then 'B'
when (成绩字段>40 and 成绩字段<60) then 'A'
else 'D' end from 成绩表