Friday, 3 October 2014

Hive Condition Clauses


1. IF( Test Condition, True Value, False Value ) 
The IF condition evaluates the “Test Condition” and if the “Test Condition” is true, then it returns the “True Value”. Otherwise, it returns the False Value.

2. COALESCE( value1,value2,... )
The COALESCE function returns the fist not NULL value from the list of values. If all the values in the list are NULL, then it returns NULL.

3.
CASE [expression]
       WHEN condition2 THEN result2
       ...
       WHEN conditionn THEN resultn
       ELSE result
END

Count distinct numbers

SELECT
count(*)
   count(DISTINCT CASE WHEN (type=2 OR type=6) THEN u ELSE NULL END)
FROM
    t
WHERE
    dt in ("2012-1-12-02", "2012-1-12-03")
GROUP BY
    type
ORDER BY
    type
;

Here expression is optional. It is the value that you are comparing to the list of conditions. 
All the conditions must be of same datatype. Conditions are evaluated in the order listed. Once a condition is found to be true, the case statement will return the result and not evaluate the conditions any further.All the results must be of same datatype. 

Reference:
http://www.folkstalk.com/2011/11/conditional-functions-in-hive.html

No comments:

Post a Comment