Hive SQL NVL correlation function

1.NVL function

NVL function has the following format: NVL (expr1, expr2)
meaning: If the value of the second parameter is the first parameter is empty oracle then displayed, if the value of the first parameter is not empty, then the first parameter would value.

Of the NVL (exprl, expr2):
. 1, null value conversion function;
2, similar to mysql-nullif (expr1, expr2) , sqlserver-ifnull (expr1, expr2).

Note:
1, if expr1 is NULL, the return value is expr2, otherwise returns expr1.
2, applicable to numeric, character, and dates, but expr2 expr1 and data type must be of the same type.

2 NVL2 function
format NVL2 function as follows: NVL2 (expr1, expr2, expr3 )
Meaning: if the first parameter is a function of the value of the second parameter is null then displayed, if the value of the first parameter is not empty, the third parameter is displayed. The SQL> SELECT ename, the NVL2 (COMM, -1,1) from EMP;
 
the ENAME the NVL2 (COMM, -1,1)
------- -----
SMITH. 1
ALLEN -1
WARD -1
JONES. 1
MARTIN -1
BLAKE. 1
CLARK. 1
SCOTT. 1
above example. Where the result is the original 1 is not empty, and the result is the original value of -1 it is empty.

 

3. NULLIF function
effect NULLIF (exp1, expr2) function is exp1 and if they are equal exp2 empty (NULL), the first value otherwise.
Below is an example. Use the oracle in the HR schema, if HR is locked, enable
role here is to show those who changed jobs in the original work, now work.
The SQL> e.LAST_NAME the SELECT, e.job_id, j.job_id, NULLIF (e.job_id, j.job_id) "Old the Job ID"
the FROM the Employees E, J the JOB_HISTORY
the WHERE e.employee_id = j.employee_id
the ORDER BY last_name;
 
the LAST_NAME the JOB_ID the Job ID Old the JOB_ID
----------------- ------- ------- -------
De Haan AD_VP IT_PROG AD_VP
Hartstein MK_MAN MK_REP MK_MAN
Kaufling ST_MAN ST_CLERK ST_MAN
Kochhar AD_VP AC_MGR AD_VP
Kochhar AD_VP AC_ACCOUNT AD_VP
Raphaely PU_MAN ST_CLERK PU_MAN
Taylor SA_REP SA_MAN SA_REP
Taylor SA_REP SA_REP
AD_ASST AC_ACCOUNT AD_ASST Whalen
Whalen AD_ASST AD_ASST
can see all employee. and equal job_id job_histroy.job_id, the output will result in air that is NULL, otherwise displays the employee. job_id

 

4.Coalesce function
role Coalese the NVL function is a function somewhat similar to its advantage is to have more options.
The following format:
Coalesce (exprl, expr2, ... .. expr3 exprn)
represents a placeholder to specify a plurality of expressions. All expressions must be of the same type, or may be implicitly converted to the same type.
The first expression returns a non-NULL expression, if the following statement:

SELECT COALESCE(NULL,NULL,3,4,5) FROM dual

Return result: 3
If all arguments are NULL, COALESCE returns NULL value.

COALESCE (expression1, ... n) CASE This function is equivalent to:

This function is actually recycled NVL is, in this example is not a child.

 

Reference: https://blog.csdn.net/qq_34941023/article/details/51440579

Guess you like

Origin www.cnblogs.com/Allen-rg/p/11099847.html