SQL devuelve múltiples valores de la declaración CASE

How do I rewrite this SQL statement to do what I need?

 SELECT * FROM TABLE 
    WHERE COLUMN_NAME_1 IN (CASE 
        WHEN COLUMN_NAME_2 = 'X' THEN 'A' 
        WHEN COLUMN_NAME_2 = 'Y' THEN 'B', 'C' END)

Obviously I can't return multiple values from a CASE clause... so how else could I write this? I am pretty sure I am slow today because this seems so easy ....

preguntado el 08 de noviembre de 11 a las 19:11

1 Respuestas

 SELECT * FROM Table WHERE
    (COLUMN_NAME_2 = 'X' AND COLUMN_NAME_1 = 'A') OR
    (COLUMN_NAME_2 = 'Y' AND COLUMN_NAME_1 IN ('B', 'C'))

or

 SELECT * FROM Table WHERE COLUMN_NAME_2 = 'X' AND COLUMN_NAME_1 = 'A'
 UNION ALL
 SELECT * FROM Table WHERE COLUMN_NAME_2 = 'Y' AND COLUMN_NAME_1 IN ('B', 'C')

Esto supone que tu , de manera want results with X or Y in COLUMN_NAME_2. If you want other rows it's not possible to tell which ones from your original SQL.

respondido 08 nov., 11:23

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.