Oracle SQL - CASE 语法

Oracle SQL - CASE syntax

假设这个查询运行良好:

SELECT 
   first_name,
   consultant_id,
   CASE consultant_id
    WHEN 1 THEN 'First Consultant'
    WHEN 2 THEN 'Second Consultant'
   END
FROM consultant
ORDER BY first_name;

除了上面两个 WHEN 子句中隐含的等于之外,为什么我不能使用布尔逻辑添加另一个 WHEN 子句?例如:

SELECT 
   first_name,
   consultant_id,
   CASE consultant_id
    WHEN 1 THEN 'First Consultant'
    WHEN 2 THEN 'Second Consultant'
    WHEN BETWEEN 3 AND 12 THEN 'Everyone else'
   END
FROM consultant
ORDER BY first_name;

抛出此错误:

ORA-00936: missing expression
00936. 00000 -  "missing expression"
*Cause:    
*Action:
Error at Line: 7 Column: 10

有没有办法在不使用更冗长的情况下完成这项工作:

SELECT 
   first_name,
   consultant_id,
   CASE 
    WHEN consultant_id = 1 THEN 'First Consultant'
    WHEN consultant_id = 2 THEN 'Second Consultant'
    WHEN consultant_id BETWEEN 3 AND 12 THEN 'Everyone else'
   END
FROM consultant
ORDER BY first_name;

是的,你是对的,很遗憾,短语法中不可能有条件。

documentation 调用短语法 "simple_case_expression"

CASE expr WHEN comparision_expr THEN return_expr

此外,根据您的要求,您可以使用:

SELECT
   first_name,
   consultant_id,
   CASE consultant_id
    WHEN 1 THEN 'First Consultant'
    WHEN 2 THEN 'Second Consultant'
    ELSE 'Everyone else'
   END
FROM consultant
ORDER BY first_name
/