【Hive】ifnull/nullif = coalesce/if

一、说明
hive中没有ifnull/nullif函数。
可用以下替代:
- coalesce( value1,value2,… )
- if( value1 is null, value2, value1)



二、举例

hive> select coalesce(col1, col2, cols) as res1, if(col1 is null, if(col2 is null, col3, col2), col1) as res2
    > from(
    >     select 1 as col1, 2 as col2, 3 as col3 
    >     union all
    >     select null as col1, 2 as col2, 3 as col3 
    >     union all
    >     select null as col1, null as col2, 3 as col3 
    >     union all 
    >     select null as col1, null as col2, null as col3
    > ) as test

结果:
    res1   res2
    1      1
    2      2
    3      3
    null   null

你可能感兴趣的:(hive)