impala条件函数最全版

x33g5p2x  于2021-12-25 转载在 其他  
字(1.5k)|赞(0)|评价(0)|浏览(556)

本文基于impala3.2版本,所有的内置条件函数;

一、条件判断函数
序号语法类型/方法名称输出类型使用说明
1isfalse (boolean expression)booleanexpression为false返回true,为true返回false
2istrue (boolean expression)booleanexpression为true返回true,为false返回false
3isnotfalse (boolean expression)booleanexpression为true返回true,为false返回false
4isnottrue (boolean expression)booleanexpression为false返回true,为true返回false
5nonnullvalue (type expression)booleanexpression为null返回false,否则返回true
6nullvalue (type expression)booleanexpression为null返回true,否则返回false
7nullif (type a, type b)和参数一致a 等于 b 则返回null, 否则返回 a
8nullifzero (type expr)和参数一致expr为0的话返回null,否则返回expr
9zeroifnull (type expr)和参数一致expr为null的话返回0,否则返回expr
10case a when b then c [when d then e…] [else f] end和参数一致根据a值返回不同的匹配结果,都不匹配返回f
11case when a then b [when c then d…] [else f] end和参数一致根据a值返回不同的匹配结果,都不匹配返回f
12coalesce(type v1, type v2, …)和参数一致得到第一个不是null的值
13decode(type exp, type s1, type r1 …type default )和参数一致exp匹配s返回对应r,否则返回default
14isnull (type a , type ifnull)和参数一致如果第一个参数为null,则返回第二个参数
15ifnull (type a , type ifnull)和参数一致如果第一个参数为null,则返回第二个参数
16nvl (type a , type b)和参数一致如果 a 为 null 则返回 b, 否则返回 a
17nvl2(type a, type b, type c)和参数一致如果a不是null则返回b, 否则返回c
18if (boolean condition , type a, type b)和参数一致表达式如果为true返回a, 否则返回b

示例如下:

--使用示例 | 对应输出 | 对应函数序号
isfalse(1>2)                                     true                        1
istrue(1>2)                                      false                       2
isnotfalse(1>2)                                  false                       3
isnottrue(1>2)                                   true                        4
nonnullvalue(null)                               false                       5 
nullvalue(null)                                  true                        6
nullif(1,1)                                      null                        7
nullifzero(0)                                    null                        8
zeroifnull(null)                                 0                           9
case 1 when 2 then 'a' when 3 then 'b'  
       else 'c' end                              c                           10
case when 1>2 then 'a' when 2>3 then 'b'
       else 'c' end                              c                           11
coalesce(null,null,1)                            1                           12
decode(1,2,'a',3,'b','c')                        c                           13
isnull(null,1)                                   1                           14
ifnull(null,1)                                   1                           15
nvl(null,1)                                      1                           16
nvl2(null,null,1)                                1                           17 
if(1>2,1,2)                                      2                           18

相关文章