What's the best way to write if/else if/else if/else in HIVE?

前端 未结 3 753
一向
一向 2020-12-15 08:05

Hive uses IF(condition, expression, expression), so when I want to do if / else if / else if / else, I have to do:

IF(a, 1, IF(b, 2, IF(c, 3, 4)))

3条回答
  •  太阳男子
    2020-12-15 08:43

    You can use Hive Conditional CASE WHEN function for if-else scenario. The CASE Statement will provide you better readability with the same functionality.

    CASE
      WHEN (condition1) THEN result1
      WHEN (condition2) THEN result2
      WHEN (condition3) THEN result3 
      WHEN (condition4) THEN result4
      ELSE result_default 
    END AS attribute_name
    

提交回复
热议问题