有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_name和subject_name代替student_id和subject_id。
如何加入这些表格? 感谢。
答案 0 :(得分: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
答案 1 :(得分: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;