Grade_table
SUBJECT |
STUDENT_ID |
GRADE |
Chemistry |
1 |
60 |
Chemistry |
2 |
65 |
Mathematics |
1 |
80 |
Mathematics |
2 |
85 |
Mathematics |
3 |
90 |
Mathematics |
4 |
95 |
Mathematics |
5 |
100 |
Physics |
1 |
85 |
Physics |
2 |
90 |
Geography |
1 |
95 |
方法一:
Select * From
(Select SUBJECT, STUDENT_ID, GRADE, ROWNUM as RN From
(Select SUBJECT,STUDENT_ID, GRADE From Grade_table
Where SUBJECT = ‘Mathematics’
Order by GRADE Desc
)
Where ROWNUM < 4 --取前三名
Order by GRADE Desc
)
Where RN = 3 --只列第三名
方法二:
Select * From
(Select SUBJECT, STUDENT_ID, GRADE,
RANK() OVER(PARTITION BY SUBJECT Order by GRADE Desc) as RANK
From Grade_table
Where SUBJECT = ‘Mathematics’
)
Where RANK < 4
Result(紅色部分):
SUBJECT |
STUDENT_ID |
GRADE |
RN |
Mathematics |
5 |
100 |
1 |
Mathematics |
4 |
95 |
2 |
Mathematics |
3 |
90 |
3 |
Result(紅色部分+綠色部分):
SUBJECT |
STUDENT_ID |
GRADE |
RN |
Mathematics |
3 |
90 |
3 |
留言列表