Skip to content
Advertisement

SQL: Making a conditional WHERE clause?

I’m pretty new to SQL and am struggling to get the syntax/logic correct for what I need.

Right now I have a query ending in a where statement:

WHERE business IN ('A','B','C')
AND
CASE
     WHEN queue = 'Q1' THEN (queue = 'Q1' AND queue_type = 'Y')
END

Basically I need all the results, except for data points that have queue = ‘Q1′. For those instances only, I need the results to filter down to where queue_type =’Y’.

As the code stands right now, I only get results where queue = ‘Q1’ and queue_type = ‘Y’.

Thanks for the help!

Advertisement

Answer

SELECT * FROM mtable
WHERE business IN ('A','B','C')
AND (queue = 'Q1' AND queue_type = 'Y') OR (queue != 'Q1')
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement