文档

CASE WHEN

更新时间:

根据value_condition的计算结果,灵活地返回不同的result值。

命令格式

MaxCompute提供以下两种case when格式:

  • case <value>
    when <value1> then <result1>
    [
      when <value2> then <result2>
      when <valuen> then <resultn>
    ]
    [
    	else <resultm>
    ]
    end
  • case
    	when (<_condition1>) then <result1>
    [
      when (<_condition1>) then <result2>
      when (<_condition1>) then <resultn>
    ]
    [
    	else <resultm>
    ]
    end

参数说明

执行判断顺序从上到下,判断值满足会直接退出。

  • value:必填。比较的值。

  • _condition:必填。指定判断条件。

  • result:必填。返回值。

返回值说明

  • 如果result类型只有BIGINT、DOUBLE,统一转为DOUBLE后,再返回结果。

  • 如果result类型中有STRING类型,则统一转为STRING后,再返回结果。如果无法进行类型转换,例如BOOLEAN类型无法转换为STRING类型,则会返回报错。

  • 不允许其他类型之间的转换。

使用示例

  • 例如表sale_detail的字段为shop_name string, customer_id string, total_price double,,包含数据如下。

    +------------+-------------+-------------+------------+------------+
    | shop_name  | customer_id | total_price | sale_date  | region     |
    +------------+-------------+-------------+------------+------------+
    | s1         | c1          | 100.1       | 2013       | china      |
    | s2         | c2          | 100.2       | 2013       | china      |
    | s3         | c3          | 100.3       | 2013       | china      |
    | null       | c5          | NULL        | 2014       | shanghai   |
    | s6         | c6          | 100.4       | 2014       | shanghai   |
    | s7         | c7          | 100.5       | 2014       | shanghai   |
    +------------+-------------+-------------+------------+------------+

    命令示例如下。

    select 
    case  
    when region='china' then 'default_region'
    when region like 'shang%' then 'sh_region'
    end as region 
    from sale_detail;

    返回结果如下。

    +------------+
    | region     |
    +------------+
    | default_region |
    | default_region |
    | default_region |
    | sh_region  |
    | sh_region  |
    | sh_region  |
    +------------+
  • 创建表并插入数据:

    --创建表
    create table mf_casewhen (id bigint,name string);
    --插入数据
    insert into table mf_casewhen 
         values(1,"a1"),
               (2,"a2"),
                     (3,"a3"),
                     (4,"a4"),
                     (5,"a5"),
                     (6,"a6"),
                     (7,"a7"),
                     (8,"a8"),
                     (9,"a9");
    • 格式一:

      select
      case
      when id<2 then 't1'
      when id<4 then 't2'
      else 't3'
      end as id_t
      from mf_casewhen;

      返回结果如下:

      +------+
      | id_t |
      +------+
      | t1   |
      | t2   |
      | t2   |
      | t3   |
      | t3   |
      | t3   |
      | t3   |
      | t3   |
      | t3   |
      +------+
    • 格式二:

      select
      case id
      when 1 then 't1'
      when 2 then 't2'
      else 't3'
      end as id_t
      from mf_casewhen;

      返回结果如下:

      +------+
      | id_t |
      +------+
      | t1   |
      | t2   |
      | t3   |
      | t3   |
      | t3   |
      | t3   |
      | t3   |
      | t3   |
      | t3   |
      +------+

相关函数

CASE WHEN函数属于其他函数,更多其他业务场景的函数请参见其他函数

  • 本页导读 (1)
文档反馈