hive 的判断条件(if、coalesce、case)

51 篇文章 1 订阅


hive 的判断条件(if、coalesce、case)
摘要: hive 里面没有ifnull 那么怎么办呢

CONDITIONAL FUNCTIONS IN HIVE

Hive supports three types of conditional functions. These functions are listed below:

IF( Test Condition, True Value, False Value ) 
The IF condition evaluates the “Test Condition” and if the “Test Condition” is true, then it returns the “True Value”. Otherwise, it returns the False Value.
Example: IF(1=1, 'working', 'not working') returns 'working'

COALESCE( value1,value2,... )

The COALESCE function returns the fist not NULL value from the list of values. If all the values in the list are NULL, then it returns NULL.
Example: COALESCE(NULL,NULL,5,NULL,4) returns 5

CASE Statement

The syntax for the case statement is:

CASE  [ expression ] 
  WHEN condition1 THEN result1 
  WHEN condition2 THEN result2 
  ... 
  WHEN conditionn THEN resultn 
  ELSE result 
END

Here expression is optional. It is the value that you are comparing to the list of conditions. (ie: condition1, condition2, ... conditionn).

All the conditions must be of same datatype. Conditions are evaluated in the order listed. Once a condition is found to be true, the case statement will return the result and not evaluate the conditions any further.

All the results must be of same datatype. This is the value returned once a condition is found to be true.

IF no condition is found to be true, then the case statement will return the value in the ELSE clause. If the ELSE clause is omitted and no condition is found to be true, then the case statement will return NULL

Example: 

CASE Fruit 
  WHEN 'APPLE' THEN 'The owner is APPLE' 
  WHEN 'ORANGE' THEN 'The owner is ORANGE' 
  ELSE 'It is another Fruit' 
END

The other form of CASE is

CASE  
  WHEN Fruit = 'APPLE' THEN 'The owner is APPLE' 
  WHEN Fruit = 'ORANGE' THEN 'The owner is ORANGE' 
  ELSE 'It is another Fruit' 
END




hive 的判断条件(if、coalesce、case)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive中,COALESCE函数用于合并数据列,并返回第一个非空值。它的语法是COALESCE(T v1, T v2, ...),其中v1、v2等是要合并的值。如果所有的值都为NULL,那么COALESCE函数将返回NULL。举个例子,如果我们执行以下查询:SELECT COALESCE(null,'100','50') FROM dual; 结果将会是100。\[2\]另外,Hive还提供了NVL函数,它的作用和COALESCE函数类似,如果值为NULL,则返回默认值。例如,执行以下查询:SELECT NVL(null,0);结果将会是0。\[1\]此外,Hive还提供了IF函数,它根据条件的真假返回不同的值。例如,执行以下查询:SELECT IF(1=2,100,200) FROM dual; 结果将会是200。\[3\] #### 引用[.reference_title] - *1* [hivecoalesce函数与nvl函数](https://blog.csdn.net/chenhua_19891230/article/details/124444638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [hive条件判断函数IF/COALESCE/CASE](https://blog.csdn.net/weixin_44839345/article/details/101546382)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值