I have a Student_classes table like following:
Student_classes
Student Classes
Jon w. Algebra
Jon w. Algebra
Jake English
Scott Spanish
Scott Korean
Neo Physics
Anderson Chemistry
Anderson Psychology
i need to fetch data from this table so that.
- if student, classes pair occurs only once, it should show up.
- if two rows have same student and classes then one should show up.
- if two students have same name but one class is spanish then only one row should be returned and row with 'spanish' should be discarded.
- if two rows have same student but different classes and none of them are 'spanish' then both the rows should show.
If all the points are covered then the following should be in the final query:
Jake English (covers point 1)
Jon w. Algebra (covers point 2)
Scott Korean (covers point 3)
Anderson Chemistry (covers point 4)
Anderson Psychology (covers point 4)
With the following query I thought i had covered all the basis but looks like it wont cover 4th point.
SELECT DISTINCT student, classes FROM student_classes
WHERE classes <> 'Spanish'
GROUP BY Student;
I have tried to take a subset of a bigger problem i am having.
Can someone please guide me towards coming up with a query that would have all 4 points covered?
I cannot change my db design.