admin管理员组

文章数量:1794759

hive if 用法

hive if 用法

1.If函数:if和case差不多,都是处理单个列的查询结果

语法: if(boolean testCondition, T valueTrue, T valueFalseOrNull)

返回值: T

说明:  当条件testCondition为TRUE时,返回valueTrue;否则返回valueFalseOrNull

举例:if(条件表达式,结果1,结果2)相当于java中的三目运算符,只是if后面的表达式类型可以不一样。

hive> select if(a=a,’bbbb’,111) fromlxw_dual;

           bbbb

hive> select if(1<2,100,200) fromlxw_dual;

          200  ————————————————  原文链接:blog.csdn/qq_26442553/article/details/79465417

if中的等于条件用“=”或“==”均可

hive (default)> select if(1==1,1,2) from t1 limit 2 ; OK _c0 1 1 Time taken: 1.384 seconds, Fetched: 2 row(s) hive (default)> select if(1<>1,1,2) from t1 limit 2 ; OK _c0 2 2 Time taken: 1.311 seconds, Fetched: 2 row(s) hive (default)> select if(1==1,if(2==2,2,3),4) from t1 limit 2 ; OK _c0 2 2 Time taken: 1.224 seconds, Fetched: 2 row(s) hive (default)> select if(1=1,1,2) from t1 limit 2 ; OK _c0 1 1 Time taken: 1.239 seconds, Fetched: 2 row(s) hive (default)> select if(1=1,if(2=2,2,3),4) from t1 limit 2 ; OK _c0 2 2 Time taken: 1.302 seconds, Fetched: 2 row(s) hive (default)> select if(1=1,if(2<>2,2,3),4) from t1 limit 2 ; OK _c0 3 3 Time taken: 1.303 seconds, Fetched: 2 row(s) hive (default)>

  

本文标签: Hive