8

I have a table with the following schema:

| ID | BUNDLE_ID | IS_ADMIN |

I need to select data from the above table having an AND clause on the basis of certain criteria, i.e

IF @FLAG = 1
    SELECT * 
    FROM TABLE A 
    WHERE A.IS_ADMIN = 1 AND BUNDLE_ID IN (3, 5)
ELSE
    SELECT * 
    FROM TABLE A 
    WHERE A.IS_ADMIN = 1 AND BUNDLE_ID IN (1, 2)

Can I achieve this in a single query?

7 Answers 7

6

General approach is to rewrite the query with a boolean expression that evaluates to what you need based on flag's value:

SELECT * FROM TABLE A WHERE A.IS_ADMIN = 1 AND ( -- Common part ...
    (@Flag=1  AND BUNDLE_ID IN(3,5))     -- IF   ...
OR
    (@Flag<>1 AND BUNDLE_ID IN(1,2))     -- ELSE ...
)
1
  • From an execution plan perspective, would this still be "clean"? (If it was searching different fields, for example, I'd say that using IF would be "better".)
    – MatBailie
    Commented Jan 23, 2018 at 11:03
5

Yes, it is simple.

SELECT *
FROM TABLE A
WHERE (A.IS_ADMIN = 1
        AND BUNDLE_ID IN (3,5)
        AND @FLAG = 1)
    OR (A.IS_ADMIN = 1
        AND BUNDLE_ID IN (1,2)
        AND (@FLAG <> 1 OR @FLAG IS NULL))
2
  • one bracket at the end is missing.
    – NiveaGM
    Commented Jan 23, 2018 at 10:45
  • @ NiveaGM, Thanks - I have updated the answer to add the bracket Commented Jan 23, 2018 at 10:47
2

Alternatives, just because...

SELECT * FROM a_table A WHERE @flag =  1 AND IS_ADMIN = 1 AND A.BUNDLE_ID IN (3, 5)
UNION ALL
SELECT * FROM a_table A WHERE @flag <> 1 AND IS_ADMIN = 1 AND A.BUNDLE_ID IN (1, 2)

Should give a similar execution plan to...

SELECT
  *
FROM
  a_table   AS A
CROSS APPLY
(
  SELECT A.BUNDLE_ID WHERE @flag =  1 AND A.BUNDLE_ID IN (3, 5)
  UNION ALL
  SELECT A.BUNDLE_ID WHERE @flag <> 1 AND A.BUNDLE_ID IN (1, 2)
)
WHERE
  A.IS_ADMIN = 1

A different layout from others answers already provided, but providing the same logic...

WITH
    admin AS
(
    SELECT * FROM a_table A WHERE IS_ADMIN = 1
)
SELECT
    *
FROM
    admin
WHERE
       (@flag =  1 AND BUNDLE_ID IN (3, 5))
    OR (@flag <> 1 AND BUNDLE_ID IN (1, 2))
2
  • I like alternatives. But you should consider the fact that @FLAG could possibly be NULL Commented Jan 23, 2018 at 12:55
  • 1
    @HoneyBadger - That would be very complete, I agree, but beyond the scope of what I was trying to communicate ;)
    – MatBailie
    Commented Jan 23, 2018 at 14:43
1

Simply:

SELECT * 
FROM   TABLE A 
WHERE  (A.IS_ADMIN = 1 AND BUNDLE_ID IN(3,5) AND @FLAG = 1)
   OR  (A.IS_ADMIN = 1 AND BUNDLE_ID IN(1,2) AND (@FLAG <> 1 OR @FLAG IS NULL))
1

You can use table driven control

SELECT A.* FROM TABLE A 
    INNER JOIN (VALUES (0,1),(0,2),(1,3),(1,5)) COND(FLAG,BUNDLE_ID)  
        ON A.BUNDLE_ID = COND.BUNDLE_ID
            AND COND.FLAG = ISNULL(@FLAG,0)
WHERE
    A.IS_ADMIN = 1  
1
  • Putting where with OR will be better in terms of performance.
    – PSK
    Commented Jan 23, 2018 at 11:28
0

This is perfectly valid syntax too...

IF @FLAG = 1
BEGIN
    SELECT * FROM TABLE A WHERE A.IS_ADMIN = 1 AND BUNDLE_ID IN(3,5)
END
ELSE
BEGIN
    SELECT * FROM TABLE A WHERE A.IS_ADMIN = 1 AND BUNDLE_ID IN(1,2)
END
-1

Try :

SELECT * FROM TABLE A 
WHERE A.IS_ADMIN = 1 AND 
(BUNDLE_ID IN (3,5) AND @FLAG = 1) OR 
(BUNDLE_ID IN (1,2) AND @FLAG <> 1)
1
  • 1
    Operator precedence warning :)
    – Arvo
    Commented Jan 23, 2018 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.