Condition "IN" at CASE WHEN ON WHERE CLAUSE?

I have a difficult situation. I want to write a sql query that includes the case when clause in the where where clause.

Just:

SELECT * FROM <table> WHERE <Column1> in CASE <Column2> WHEN 1 THEN ('OP', 'CL') WHEN 0 THEN ('RE', 'ST') END 

Column1 must be "in", not "=". Because there are several values โ€‹โ€‹for column 1. This query returns "Invalid syntax near", ".". error.

Can you give me some suggestion? (Sorry for my bad English.)

EDITOR: I think I misunderstood. If column 2 is 1, the condition must match the value "IN (" OP "," CL ")" else Column 1 is 2, the condition must match the value "IN (" RE "," ST ")".

+4
source share
2 answers

You do not need a CASE expression for this; you can simply use OR as follows:

 SELECT * FROM <table> WHERE (Column2 = 1 AND Column1 IN ('OP', 'CL')) OR (Column2 = 0 AND Column1 IN ('RE', 'ST')) 
+6
source
 Select * from table where <Column1> in (Select case <Column2> when 1 then ('OP','CL') when 0 then ('RE','ST')) 
-1
source

Source: https://habr.com/ru/post/1434186/


All Articles