hive函数

victorzhzh 2019-12-03

hive条件函数

(1)if函数:if(条件表达式,结果1,结果2)

hive> select if(1=2,100,200) from lxw_dual;
200
hive> select if(1=1,100,200) from lxw_dual;
100

(2)coalesce函数:返回参数中的第一个非空值;如果所有值都为null,那么返回null

hive> select COALESCE(null,‘100‘,‘50′) from lxw_dual;
100

(3)case when函数:

  •  CASE a WHEN b THEN c [WHENd THEN e]* [ELSE f] END
  • 如果a等于b,那么返回c;如果a等于d,那么返回e;否则返回f
hive> Select case 100 when 50 then ‘tom‘ when 100 then ‘mary‘ else ‘tim‘ end from lxw_dual;
mary
hive> Select case 200 when 50 then ‘tom‘ when 100 then ‘mary‘ else ‘tim‘ end from lxw_dual;
tim
  • CASE WHEN a THEN b [WHEN cTHEN d]* [ELSE e] END
  • 如果a为TRUE,则返回b;如果c为TRUE,则返回d;否则返回e
hive> select case when 1=2 then ‘tom‘ when 2=2 then ‘mary‘ else ‘tim‘ end from lxw_dual;
mary
hive> select case when 1=1 then ‘tom‘ when 2=2 then ‘mary‘ else ‘tim‘ end from lxw_dual;
tom

相关推荐