1

I have the following SQL query where the CASE statement needs to return multiple values.

select TASK_NAME 
from TASK 
where TASK_TYPE in (          
    case 
        when (select USER_COUNTRY from USER) in (select COUNTRY_NAME from COUNTRY where CONTINENT = 'Europe') then ('Adhoc', 'Resell')
        when (select USER_COUNTRY from USER) in (select COUNTRY_NAME from COUNTRY where CONTINENT = 'Americas') then ('Classified', 'Bought', 'Handle')
        else ('None', 'Blank')
    end )

However, the number of WHEN clauses is not fixed as there may be more conditions. I don't think SQL return multiple values from CASE statement will work.

I should also add that grouping is not recognised by the application which this query is passed to.

I apologise for not providing the tables. I will do so in due course. Just wanted to get this out there.

1
  • What do you mean by "needs to return multiple values?" Case statements by design function in the following manner - the first condition to be true will be returned. It sounds like you need a join, not a CASE. Commented Jan 28, 2015 at 15:41

1 Answer 1

2

You can move the comparison to the when clauses and have the case return a scalar value indicating a match:

where 1 = (case when (select USER_COUNTRY from USER) in (select COUNTRY_NAME from COUNTRY where CONTINENT = 'Europe') and Task_Type IN ('Adhoc', 'Resell')
                then 1
                when (select USER_COUNTRY from USER) in (select COUNTRY_NAME from COUNTRY where CONTINENT = 'Americas') and Task_Type IN ('Classified', 'Bought', 'Handle')
                then 1
                when Task_Type IN ('None', 'Blank')
                then 1
            end )

This is a rather strange set of conditions. Are you sure you can't express the logic better using JOIN.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, the use of groups and joins are limited in the application. In fact. the query in my original post is a subquery.
@Confounder . . . My actual concern is that USER might have more than one row. In that case, the SQL would typically generate an error.

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.