參考hive常用運算。
•If函數: if
•非空查找函數: COALESCE
•條件判斷函數:CASE
• If 函數 : if
語法: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
返回值: T
說明: 當條件testCondition為TRUE時,返回valueTrue;否則返回valueFalseOrNull
舉例:
hive> select if(1=2,100,200) from dual;
200
hive> select if(1=1,100,200) from dual;
100
• 非空查找函數 : COALESCE
語法: COALESCE(T v1, T v2, …)
返回值: T
說明: 返回參數中的第一個非空值;如果所有值都為NULL,那么返回NULL
舉例:
hive> select COALESCE(null,'100','50′) from dual;
100
條件判斷函數: CASE
語法 : CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
返回值 : T
說明:如果 a 等於 b ,那么返回 c ;如果 a 等於 d ,那么返回 e ;否則返回 f
舉例:
hive> Select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end from dual;
實例:
insert overwrite table branch_atmzc Select canal, XT_OP_TRL, SA_TX_DT,if(dr_cr_cod = '2',-abs(cr_tx_amt),cr_tx_amt) as cr_tx_amt2 from branch_atm group by canal,XT_OP_TRL , SA_TX_DT,dr_cr_cod,cr_tx_amt;
insert overwrite table branch_atmzc Select canal, XT_OP_TRL, SA_TX_DT,case when (tran_cd = '1062' or tran_cd = '5069') then '取款' when (tran_cd = '5148' or tran_cd = '1061') then '存款' when (tran_cd ='5069') then '手續費' when (tran_cd ='5076' or tran_cd = '5922' or tran_cd = '3806') then '轉賬' else '其他' end as tran_cd, sum(cr_tx_amt),sum(counts) from branch_atm group by canal, XT_OP_TRL , SA_TX_DT,tran_cd ;