I tried Using CASE WHEN statement,But it seems as if i missed something or this is not the correct way of doing it.
@APASS parameter makes the query to either return those who passed or those who failed the specified exam.
SELECT ID, Name,ZipCode,Mobile
FROM tblStudent
WHERE (Sex = @Sex) AND (ID IN
(SELECT StID
FROM tblTest
WHERE (TestID = @TestID) AND
CASE WHEN @APass = TRUE THEN (Score IN (27, 28, 29, 30))
ELSE (Score NOT IN (27, 28, 29, 30))
END
GROUP BY StID, TestID
HAVING (COUNT(*) = @Times)))
In your case how about:
Or you could create two separate queries, one for pass, and one for fail. This is probably the most efficient if you care about performance.
You could also do something cute like:
But again I wonder if this will truly give you an optimized query.