在
oracle
中有一数据表
exam_result
(成绩记录表),
表中的一条记录描述了
“
某个班某个学生某次考试的成绩
"
create table EXAM_RESULT
(
ID
NUMBER(10) not null,
--
主键
CLASSID NUMBER(10) not null,
--
班级
id
,关联到班级表
USERID
NUMBER(10) not null,
--
用户
id
,关联到用户表
EXAMID
NUMBER(10) not null,
--
试卷
id
,关联到试卷表
RESULT
NUMBER(3)
--
成绩
)
现在要求统计完成了试卷
id
为
1,2,3
的成绩的前
3
名
即完成了试卷
id
为
1
的前
3
名
,
完成了试卷
id
为
2
的前
3
名
,
完成了试卷
id
为
3
的前
3
名
select * from (
select
e.classid,
e.userid,
e.examid,
e.result,
row_number() over (partition by e.examid order by e.examid, e.result desc) rn
from exam_result e
where e.examid in (1,2,3)
) where rn <= 3