I have a table containing association data. It has three columns SubjectId, StudentId and Rank. It is a many to many relation table. I need to find the SubjectId corresponding to a list of given StudentIds in rank order.
If the order was unimportant, I would do something like
SELECT SubjectId
FROM mytable
WHERE StudentId IN (a,b,c) --This is the 'input'. I want order here to make a difference by matching to 'rank' but in this version it doesn't
GROUP BY SubjectId
HAVING COUNT(1) = 3
This would also return SubjectIds with ranks of students in order (a, c, b), (b, c, a), etc.
How would I do the same if the order of a, b, c is important?
Edit: included sample data and expected result
Sample data
SubjectId | StudentId | Rank
1|a|2
1|c|1
1|b|3
2|c|1
2|b|2
3|c|2
3|b|1
4|a|1
4|b|2
5|b|2
5|a|1
Expected results Input: (c,a,b) Output: 1
Input: (c,a) Output: NULL (no rows)
Input: (c,b) Output: 2
Input: (a,b) Output: 4, 5 --as separate rows
Input: (a,b,c) Output: NULL (no rows)