0

I need a nested case structure in a query which the outer case uses IN operator. I used same inner case part in all conditions of outer case as shown in the following example:

SELECT
    CASE
        WHEN
            (CASE
                WHEN expression1 THEN value1
                WHEN expression2 THEN value2
                ELSE value3
            END) in (1, 2)
            THEN 'A'
        WHEN
            (CASE
                WHEN expression1 THEN value1
                WHEN expression2 THEN value2
                ELSE value3
            END) in (3, 4)
            THEN 'B'
        ELSE 'C'
    END

That example works well but I want to use it as follows if possible:

SELECT
    CASE 
        (CASE
                WHEN expression1 THEN value1
                WHEN expression2 THEN value2
                ELSE value3
        END)

        WHEN
            an_alias_if_necessary in (1, 2)
            THEN 'A'
        WHEN
            an_alias_if_necessary in (3, 4)
            THEN 'B'
        ELSE 'C'
    END

The following example of that nested case which does not have IN operator works well. Is it possible to use same structure with IN operator ?

SELECT
    CASE 
        (CASE
                WHEN expression1 THEN value1
                WHEN expression2 THEN value2
                ELSE value3
        END)

        WHEN asked_value1 THEN 'A'
        WHEN asked_value2 THEN 'B'
        ELSE 'C'
    END

2 Answers 2

1

PostgreSQL does not support the SQL standard feature “Comma-separated predicates in simple CASE expression” (F263), so you cannot do that.

You'll have to use a construct like:

CASE CASE
        WHEN expression1 THEN value1
        WHEN expression2 THEN value2
        ELSE value3
     END
   WHEN 1 THEN 'A'
   WHEN 2 THEN 'A'
   WHEN 3 THEN 'B'
   WHEN 4 THEN 'B'
   ELSE 'C'
END
Sign up to request clarification or add additional context in comments.

5 Comments

You could probably hack your way around it by using ...IN(VALUES(1,2,3)) (maybe needs a SELECT, too)
@wildplasser Can you give a complete example?
BTW: list of scalars: postgresql.org/docs/10/static/…
@wildplasser I don't understand how these could be used in a CASE expression without repeating the left side expression, which the OP seeks to avoid.
I thought any condition is valid, iff it yields a boolean.Exists(...) does work. Maybe an extra set of() is needed? (working on it)
0

It seems you need to use a VALUES(1,2) imediate table


SELECT
        topic_id
        ,krant_id
        , CASE WHEN (krant_id IN (SELECT val FROM (VALUES(1),(2) ) vals(val)))THEN 'OneTwo' ELSE 'Normal' END AS label
FROM react
WHERE react_date >= '2011-01-01'
AND react_date < '2011-02-01'
        ;

Tucking the VALUES() into a CTE could simplify the query a bit:


WITH vals(val) AS (VALUES(1),(2))
SELECT
        topic_id
        ,krant_id
        , CASE WHEN (krant_id IN (SELECT val FROM vals)) THEN 'OneTwo' ELSE 'Normal' END AS label
FROM react
WHERE react_date >= '2011-01-01'
AND react_date < '2011-02-01'
        ;

Sorry, I used my own existing table. The syntax is correct,though.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.