Hive条件判断 if,coalesce,case…when
hive中可能会遇到根据判断不同值,产生对应结果的场景,有三种实现方式:
IF( Test Condition, True Value, False Value )
COALESCE( value1,value2,… )
CASE Statement
事例及说明
IF( Test Condition, True Value, False Value )
该语法只能用来判断单个条件,例如:
select pd,
if(ps_t='常年',1,0) as pt,
ps_t
from dgp limit 100;
- 1
- 2
- 3
- 4
- 5
COALESCE( value1,value2,… )
该函数用来获取参数列表中的首个非空值,若均为NULL,则返回NULL,例如:
select
coalesce(NULL,NULL,5,NULL,1,0) as pt;
- 1
- 2
CASE Statement
该语法可以与某字段多个比较值的判断,并分别产生不同结果,与其他语言中case语法相似,例如:
select pd,
case pst
when "常年" then 1
when "非常年" then 0
else 0
end
as pt
from dgp limit 100;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
或:
select pd,
case
when pst="常年" then 1
when pst="非常年" then 0
else 0
end
as pt
from dgp limit 100;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
望:记录、记忆、传播、帮助。
转载:https://blog.csdn.net/u012378570/article/details/62216722