我有以下SQL查询,其中CASE语句需要返回多个值。
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 )
然而,WHEN条款的数量并不固定,因为可能有更多条件。我认为SQL return multiple values from CASE statement不起作用。
我还应该补充说,传递此查询的应用程序无法识别分组。
我为没有提供表格而道歉。我会在适当的时候这样做。只是想把它拿出来。
答案 0 :(得分:2)
您可以将比较移至when
子句,并让case
返回表示匹配的标量值:
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 )
这是一组相当奇怪的条件。您确定无法使用JOIN
更好地表达逻辑。