我有两张桌子。
Student (Roll_id,Student_name)
Student_mark (roll_id, semester, marks, subject)
我想要学生的详细信息,这些学生在学期和主题方面获得最高分,他们的roll_id,mark,semester,subject 使用那两个表。
我们可以在不使用游标的情况下获得结果吗?。
答案 0 :(得分:0)
您可以使用RANK()功能按学生顺序排列学生(按学期和科目分区):
WITH RankedResults AS
( SELECT s.Roll_ID,
s.Student_Name,
sm.Semester,
sm.Subject,
sm.Marks,
StudentRank = RANK() OVER(PARTITION BY sm.Semester, sm.Subject ORDER BY sm.Marks DESC)
FROM Student s
INNER JOIN Student_Mark sm
ON s.Roll_ID = sm.Roll_ID
)
SELECT Roll_ID, Student_name, Semester, Subject, Marks
FROM RankedResults
WHERE StudentRank = 1;
<强> Example on SQL Fiddle 强>