Home Forums SQL Server 2012 SQL Server 2012 - T-SQL How to return list of all courses for which a student meets all the prerequisites. RE: How to return list of all courses for which a student meets all the prerequisites.

  • I must have munged the SQL somewhere... it's not omitting the "next" courses for which a given student has some but not all of the prerequisites.

    Here's the SQL I have, which runs, but returns the wrong answer...

    DECLARE @StudentID INT = 1;

    SELECT @StudentId AS StudentID

    , C.CourseID

    FROM Course C

    WHERE NOT EXISTS (SELECT 1

    FROM Prereq P LEFT JOIN Grades G ON P.ReqCourseID = G.CourseID

    WHERE NextCourseID = C.CourseID

    AND G.StudentID = @StudentID

    AND IsNULL(G.Grade,0) < 2

    )

    AND NOT EXISTS (

    SELECT 1

    FROM Grades

    WHERE Grade>=2 -- 2 is considered passing.

    AND StudentID = @StudentID

    AND CourseID = C.CourseID)

    Sorry... I'll come back to this and add some minimal number of records. The hard part is picking SOME of the course hierarchy (I'll start at the bottom.). So I'll work on that and post hopefully by tomorrow night. Then I can figure out what's going on.