2017-07-22 66 views

回答

0

試試這個

select student.student_id,student.student_name,department.department_name,mark.value 
from student left join department on student.department_id=department.department_id 
left join mark on student.student_id=mark.student_id 
left join subject on subject.subject_id=mark.subject_id 
where subject.subject_name='Maths' 
order by department_name , mark.value desc ; 

但如果你想每一個部門,你可以用「與條款」或子查詢類似下面的前10名:

select * from (

    select student.student_id,student.student_name,department.department_name,mark.value 
    ,row_number() over(partition by department.department_id order by mark.value desc) as rowNumber 
    from student left join department on student.department_id=department.department_id 
    left join mark on student.student_id=mark.student_id 
    left join subject on subject.subject_id=mark.subject_id 
    where subject.subject_name='Maths' 

) studentData where rowNumber<=10 ; 
相關問題