# Conditional,Numeric And Mathematical Functions In Hive

Views:

Category: Education

## Presentation Transcript

### CONDITIONAL , NUMERIC AND MATHEMATICAL FUNCTIONS IN HIVE:

CONDITIONAL , NUMERIC AND MATHEMATICAL FUNCTIONS IN HIVE http://www.orienit.com/

### CONDITIONAL FUNCTIONS IN HIVE :

Example: COALESCE(NULL,NULL,5,NULL,4) returns 5 CONDITIONAL FUNCTIONS IN HIVE Hive supports three types of conditional functions. These functions are listed below: 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. 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. Example: IF(1=1, 'working', 'not working') returns ' working‘c http://www.orienit.com/

### Slide 3:

CASE Statement The syntax for the case statement is: CASE [ expression ] WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... WHEN conditionn THEN resultn ELSE result END Here expression is optional. It is the value that you are comparing to the list of conditions. ( i.e : condition1, condition2, ... conditionn). 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. http://www.orienit.com/

### Slide 4:

All the results must be of same datatype. This is the value returned once a condition is found to be true. IF no condition is found to be true, then the case statement will return the value in the ELSE clause. If the ELSE clause is omitted and no condition is found to be true, then the case statement will return NULL Example:  CASE Fruit WHEN 'APPLE' THEN 'The owner is APPLE' WHEN 'ORANGE' THEN 'The owner is ORANGE' ELSE 'It is another Fruit' END CASE WHEN 'APPLE' THEN 'The owner is APPLE' WHEN 'ORANGE' THEN 'The owner is ORANGE' ELSE 'It is another Fruit' END The other form of CASE is http://www.orienit.com/

### NUMERIC AND MATHEMATICAL FUNCTIONS IN HIVE :

NUMERIC AND MATHEMATICAL FUNCTIONS IN HIVE The Numerical functions are listed below in alphabetical order. Use these functions in SQL queries.  ABS ( double n ) The ABS function returns the absolute value of a number. ACOS ( double n ) The ACOS function returns the arc cosine of value n. This function returns Null if the value n is not in the range of -1<=n<=1. Example: ABS(-100) Example: ACOS(0.5) http://www.orienit.com/

### Slide 6:

BIN ( bigint n ) The BIN function returns the number n in the binary format. Example: BIN(100) E Example: CEIL(9.5) CEIL ( double n ),  CEILING ( double n ) The CEILING or CEILING function returns the smallest integer greater than or equal to the decimal value n. CONV ( bigint n, int from_base, int to_base ) The CONV function converts the given number n from one base to another base. EXAMPLE: CONV(100, 10,2) http://www.orienit.com/

### Slide 7:

COS ( double n )  The COS function returns the cosine of the value n. Here n should be specified in radians. Example: COS(180*3.1415926/180) Example: EXP(50) EXP ( double n ) The EXP function returns e to the power of n. Where e is the base of natural logarithm and its value is 2.718. FLOOR ( double n ) The FLOOR function returns the largest integer less than or equal to the given value n. Example: FLOOR(10.9) http://www.orienit.com/

### Slide 8:

HEX ( bigint n) This function converts the value n into hexadecimal format. Example: HEX(16) Example: HEX(‘ABC’) HEX ( string n ) This function converts each character into hex representation format. LN ( double n ) The LN function returns the natural log of a number. Example: LN(123.45) ) LOG ( double base, double n ) The LOG function returns the base logarithm of the number n. Example: LOG(3, 66) http://www.orienit.com/

### Slide 9:

LOG2 ( double n ) The LOG2 function returns the base-2 logarithm of the number n. Example: LOG2(44) Example: LOG10(100) LOG10 ( double n ) The LOG10 function returns the base-10 logarithm of the number n. NEGATIVE ( int n ),   NEGATIVE ( double n )  The NEGATIVE function returns –n Example: NEGATIVE(10) PMOD ( int m, int n ),  PMOD ( double m, double n )  The PMOD function returns the positive modulus of a number. Example: PMOD(3,2) http://www.orienit.com/

### Slide 10:

POSITIVE ( int n ),  POSITIVE ( double n ) The POSITIVE function returns n Example: POSITIVE(-10) Example: POW(10,2) POW ( double m, double n ),  POWER ( double m, double n ) The POW or POWER function returns m value raised to the n power. RAND ( [int seed] ) The RAND function returns a random number.If you specify the seed value, the generated random number will become deterministic. Example: RAND( ) ROUND ( double value [, int n] ) The ROUND function returns the value rounded to n integer places. Example: ROUND(123.456,2) http://www.orienit.com/

### Slide 11:

SIN ( double n )  The SIN function returns the sin of a number. Here n should be specified in radians. Example: SIN(2) ) Example: SQRT(4) SQRT ( double n ) The SQRT function returns the square root of the number UNHEX ( string n ) The UNHEX function is the inverse of HEX function. It converts the specified string to the number format. Example: UNHEX(‘AB’) http://www.orienit.com/

### Slide 12: 