
Hive SQL-条件函数汇总
Contents
已经2021年了,SQL条件函数已不局限于case when 了,针对常见场景已新增许多 conditional functions
汇总 Hive SQL 常用的条件函数,👇

if(condition, value True, value FalseOrNull)
This is the one of best Hive Conditional Function
类似Excel中的 if 函数
|
|
case when then end
case when 较为常见,可注意执行顺序问题:从上至下依次执行,直到满足条件则跳出
常见写法有两种,主要取决于是否单变量作判断
|
|
isnull(a)
判断是否为null(数据库特有的一种数据类型),留意字符型 'null', 'NULL'
|
|
isnotnull(a)
非null
|
|
nullif(a,b)
如果 a=b 则返回null,否则返回a
等同于 case when a = b then null else a end
|
|
nvl(arg1,arg2)
可以理解为用arg2的值替换 arg1 中值为 null 的部分
|
|
coalesce(value1,value2,…)
返回第一个非null的值
往往只想得到众多列中非 null 的值
|
|
涉及null的问题要留意数据库中的数据类型。譬如,某列数据格式为string,'null' 并不等同于 null
胡子叔叔的小站