Domanda

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)))
È stato utile?

Soluzione

In your case how about:

WHERE TestID = @TestID
AND 
(
    (@APass = 'TRUE' AND Score IN (27, 28, 29, 30))
    OR (@APass = 'FALSE' AND Score NOT IN (27, 28, 29, 30))
)

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:

WHERE TestID = @TestID
AND @APass=IF(Score IN (27, 28, 29, 30),'TRUE','FALSE')

But again I wonder if this will truly give you an optimized query.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top