There are students of different departments and classes that take different exams repeatedly based on certain requisites/rules. I need to pull the latest grades based on examdate for students of classid and departmentid that are passed to the stored procedure during execution.
Here is my query:
SELECT @MathGrade=MathGrade, @EngGrade=EngGrade
FROM MathHistory MH,StudentRecordSR, ExamHistoryMinimumRule EHMR
where MH.ClassID=SR.ClassID AND SR.DepartmentID=@DepartmentID
AND SR.StudentID=@StudentID
AND EHMR.MinimumRuleRESULT=1
AND MH.EXAMTYPEID IN (2)
AND EHMR.MinimumRuleID IN (1,2,6)
AND MH.ExamDate=(SELECT MAX(ExamDate) FROM MathHistory)
This query gives me the right results until I add the last AND condition to get the gradde for most recent exam date.
Can you help me?