简体   繁体   中英

How to get max value with 3 inner joins?

Need help with getting the max value for Grade with inner joins.


Code:

SELECT StudFirstName, StudLastName, sc.ClassID, sub.SubjectID, sub.CategoryID, Grade
FROM SS_Students as st INNER JOIN SS_Student_Schedules as ss
ON st.StudentID = ss.StudentID
INNER JOIN SS_Classes as sc
ON ss.ClassID = sc.ClassID
INNER JOIN SS_Subjects as sub
ON sc.SubjectID = sub.SubjectID;

Just need to get the max value for Grade so the output will shorten down to 1 row.

You should use group by and max like:

SELECT StudFirstName, StudLastName, sc.ClassID, sub.SubjectID, sub.CategoryID, MAX(Grade)
FROM SS_Students as st INNER JOIN SS_Student_Schedules as ss
    ON st.StudentID = ss.StudentID
INNER JOIN SS_Classes as sc
    ON ss.ClassID = sc.ClassID
INNER JOIN SS_Subjects as sub
    ON sc.SubjectID = sub.SubjectID
GROUP BY StudFirstName, StudLastName, sc.ClassID, sub.SubjectID, sub.CategoryID

This will give you the student list and max grade for each student.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM