SQL problem

Albatross

Platinum Member
Jul 17, 2001
2,344
8
81
http://sqlfiddle.com/#!2/17bd34/158

So i need to select Bob and Jan with class 1 and 2 respectively.
But class1 demands courses 3 and 4 and course 4 has prerequisites class 1 and 3 so i need to select only course 3 which has one prerequisite which is class 1.

Same for Jan:class 2 demands courses 5 and 8 but 8 also demands class 6 so only class 5 should be selected.
So these are expected results:

Bob | Class 3
Jan | Class 5

I only managed to somehow isolate the problem now I dont know how to continue so that the query will work for people with multiple requirements also,because I could do a GROUP BY HAVING COUNT(*)=1 and this will get my results but this is an accident.Thanks.
 

douglasb

Diamond Member
Apr 11, 2005
3,157
0
76
You could have another table for just the prerequisite courses.

So, for your example:
Code:
Class  | Prerequisite
-------+-------------
4      |  1
4      |  3
3      |  1
5      |  2
8      |  6
8      |  5

Assuming that you have a table with each student and the classes that he/she has already taken, then you can see which classes have all of the prerequisites filled for a particular student.

EDIT: I may have misunderstood your question. Looking at the fiddle, it looks like you already have that covered.
 

KB

Diamond Member
Nov 8, 1999
5,402
386
126
Your schema needs to be a bit more complicated. I think the simple schema actually makes the problem more difficult.

You need a lot more tables, plus identity rows, foriegn keys etc.

Code:
CREATE TABLE Student (StudentID int, Name CHAR(50));
CREATE TABLE Course (CourseID int, Name CHAR(50));
CREATE TABLE StudentCourse (StudentCourseID int, CourseID int, StudentID int);
CREATE TABLE Prerequisite (CoursePreReqsID int, CourseID int, PreCourseID int);

INSERT INTO Student VALUES (1, 'Bob');
INSERT INTO Student VALUES (2, 'Jan');

INSERT INTO Course VALUES(1, 'Class 1');
INSERT INTO Course VALUES(2, 'Class 2');
INSERT INTO Course VALUES(3, 'Class 3');
INSERT INTO Course VALUES(4, 'Class 4');
INSERT INTO Course VALUES(5, 'Class 5');
INSERT INTO Course VALUES(6, 'Class 6');
INSERT INTO Course VALUES(7, 'Class 7');
INSERT INTO Course VALUES(8, 'Class 8');
INSERT INTO Course VALUES(9, 'Class 9');

INSERT INTO StudentCourse VALUES(1, 1, 1);
INSERT INTO StudentCourse VALUES(2, 2, 2);

INSERT INTO Prerequisite VALUES(1, 3, 1);
INSERT INTO Prerequisite VALUES(2, 6, 4);
INSERT INTO Prerequisite VALUES(3, 4, 1);
INSERT INTO Prerequisite VALUES(4, 4, 3);
INSERT INTO Prerequisite VALUES(5, 8, 2);
INSERT INTO Prerequisite VALUES(6, 8, 6);
INSERT INTO Prerequisite VALUES(7, 5, 2);
INSERT INTO Prerequisite VALUES(8, 9, 8);
INSERT INTO Prerequisite VALUES(9, 7, 5);

Heres the ugly query. Someone might be able tosimplify it, but it is a rather difficult problem for SQL. SQL likes to match things, not find unmatched things.


Code:
SELECT Course.CourseID, Course.Name, Student.StudentID, Student.Name
FROM     Course INNER JOIN
                  Prerequisite ON Course.CourseID = Prerequisite.CourseID LEFT JOIN
                  StudentCourse ON Prerequisite.PreCourseID = StudentCourse.CourseID LEFT JOIN
                  Student ON StudentCourse.StudentID = Student.StudentID LEFT JOIN
				  (SELECT Course.CourseID, Course.Name As Course, s.StudentID, s.Name
					FROM     Course INNER JOIN
                  Prerequisite ON Course.CourseID = Prerequisite.CourseID LEFT JOIN
                  StudentCourse ON Prerequisite.PreCourseID = StudentCourse.CourseID LEFT JOIN
                  Student ON StudentCourse.StudentID = Student.StudentID CROSS JOIN Student s
				  WHERE Student.StudentID IS NULL) Incomplete ON Student.StudentID = Incomplete.StudentID AND Course.CourseID = Incomplete.CourseID
WHERE Incomplete.CourseID IS NULL AND Student.StudentID IS NOT NULL
 

douglasb

Diamond Member
Apr 11, 2005
3,157
0
76
^ Agree on the schema being too simple. You could write a query for the simple schema in the 1st fiddle and it would work, but it would be a nightmare.
 

Albatross

Platinum Member
Jul 17, 2001
2,344
8
81
Thank you all.I agree that is an autistic schema but it`s not mine,it`s a problem I encountered online so I was curious if anyone could solve it.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |