2014-09-24 199 views
-3

3表是有,一個是學生,第二個是主體和第三個是馬克表。如何查詢以獲得此結果?

student table: 
    student_id student_name 

subject table: 
    subject_id subject_name 

marks table: 
    student_id subject_id marks 

我寫此查詢:

select student_id, subject_id, marks, RANK() over (partition by subject_id order by marks desc rank 
from marks order by student_id, subject_id; 

它給正確的結果,但我想到位student_id數據和subject_id的student_name和SUBJECT_NAME。

如何加入這些表? 謝謝。

+5

你編寫使用'排名在Partition'查詢,但你不理解一個簡單的'加入'概念...? – Siyual 2014-09-24 13:15:35

回答

0

只需添加一對夫婦的join條款:

SELECT student_name, subject_name, marks, 
     RANK() OVER (PARTITION BY subject_name ORDER BY marks DESC) rk 
FROM  marks 
JOIN  student ON marks.student_id = student.student_id 
JOIN  subject ON marks.subject_id = subject.subject_id 
ORDER BY student_name, subject_name 
+0

對不起,沒有更新的行屬性格式這就是爲什麼只給2 rows.Now其給出結果正確 – swapprose 2014-09-24 13:56:02

0

這可以通過簡單的做加盟

select student_name, subject_name, marks, RANK() over (partition by subject_id order by marks desc) rank 
    from marks 
    inner join student on mark.student_id=student.student_id 
    inner join subject on mark.subject_id=subject.subject_id 
    order by student_id, subject_id; 
+0

它給錯誤曖昧student_id數據,subject_id – swapprose 2014-09-24 13:47:20

相關問題