Вы можете использовать аналитическую функцию следующим образом:
Select id,
first_name,
year_id,
dept_id,
total,
PER
from
(SELECT T1.id,
T1.first_name,
T1.year_id,
T1.dept_id,
T1.total,
( T1.total / 300 ) * 100 PER ,
Dense_rank() over (partition by year_id, dept_id order by T1.total desc) as rn
FROM (SELECT S.stu_id ID,
first_name,
last_name,
year_id,
dept_id,
eng,
maths,
science,
( eng + maths + science ) TOTAL
FROM stu.student S,
stu.result R
WHERE S.stu_id = R.stu_id) T1) where rn = 1;
- обновление -
Вы можете добиться этого, используя join и cte с группой, следующим образом:
With T1 as
(SELECT S.stu_id ID,
first_name,
last_name,
year_id,
dept_id,
eng,
maths,
science,
( eng + maths + science ) TOTAL
FROM stu.student S,
Join stu.result R
On (S.stu_id = R.stu_id))
Select S.ID, S.FIRSTNAME, T2.YEAR_ID, T2.DEPT_ID,
T2.TOTAL, T2.TOTAL/3 AS PER
FROM
(Select year_id,
dept_id,
Max(total) as total
From t1
Group by year_id,
dept_id) T2
JOIN T1 ON (T2.TOTAL = S.TOTAL
AND T2.DEPT_ID = S.DEPT_ID
AND T2.YEAR_ID = S.YEAR_ID);
Ура !!