VAR_SAMP
Purpose
This function returns the variance of expr
within a window or group of rows. This equates to the following formula:
Syntax
var_samp::=
Usage Notes
- With
DISTINCT
, the function considers duplicate values ofexpr
only once. It considers all occurrences withALL
. If the function contains neitherALL
norDISTINCT
, the default isALL
. VAR_SAMP
function is identical to VARIANCE. However, if a window or group has only one row, then the result of this function isNULL
instead of0
.
Examples
Aggregate Function
Result
DEPARTMENT | VAR_SAMP |
ACCOUNTS | 1.125e+08 |
HR | 2.0625e+08 |
Analytic Function
SELECT
id, department, hire_date, current_salary,
VAR_SAMP(current_salary) OVER (PARTITION BY department ORDER BY hire_date) VAR_SAMP
FROM employee_table ORDER BY department, hire_date;
Result
ID | DEPARTMENT | HIRE_DATE | CURRENT_SALARY | VAR_SAMP |
2005 | ACCOUNTS | 2013-01-01 | 80000 | NULL |
2003 | ACCOUNTS | 2015-07-01 | 80000 | 0 |
2002 | ACCOUNTS | 2017-01-01 | 65000 | 56250000 |
2004 | ACCOUNTS | 2017-01-01 | 70000 | 56250000 |
2001 | ACCOUNTS | 2018-07-01 | 55000 | 1.125e+08 |
1003 | HR | 2014-01-01 | 90000 | NULL |
1002 | HR | 2016-01-01 | 70000 | 1.3333333e+08 |
1004 | HR | 2016-01-01 | 70000 | 1.3333333e+08 |
1001 | HR | 2018-01-01 | 55000 | 2.0625e+08 |