我从最大列获取行时遇到问题, 这里的插图:http://sqlfiddle.com/#!3/15207/9
基本上我想获得学生的最高成绩以及成绩id(gid)。但查询不正确,因为使用的最高等级ID将返回2.
第一门课程,学生得到D(1分)。 第二次上课,学生得到E(0分)。
该1分的成绩是1。
我希望查询结果为:1(userid),1(subjectid),1(gradeid),1(得分)
由于
更新01:
答案 0 :(得分:3)
更新1
WITH highestScore
AS
(
SELECT a.uid StudentID,
a.uname StudentName,
b.Score,
c.cname CourseName,
c.semester,
d.sname SubjectName, d.sid,
DENSE_RANK() OVER (PARTITION BY a.uid, d.sid
ORDER BY b.Score DESC) rn
FROM Users a
INNER JOIN Grades b
ON a.uid = b.uid
INNER JOIN Courses c
ON b.cid = c.cid
INNER JOIN Subjects d
ON c.sid = d.sid
)
SELECT StudentID, StudentName, CourseName, semester,
SubjectName, Score
FROM highestScore
WHERE StudentID = 1 AND -- StudentID
sid = 1 AND -- SubjectID
RN = 1 -- leave this as is (rank of the highest score)