Purpose
This function calculates the sample standard deviation of numeric data. The sample standard deviation is the square root of the sample variance. The difference between STDDEV_SAMP and the STDDEV function is that when STDDEV is given only one row of input data, it returns 0, while STDDEV_SAMP returns NULL. It can be used as an aggregate or analytic function.
Note
- When used as an analytic function, you must use the
OVERclause to define the window for calculation. It calculates the values for a set of rows and returns multiple values. - When used as an aggregate function, it aggregates the values for a set of rows and returns only one value. In this case, the
OVERclause is not required.
Syntax
STDDEV_SAMP([ALL] expr) [ OVER (analytic_clause) ]
Parameters
Parameter |
Description |
|---|---|
| ALL | All numeric columns. Optional. Default value is ALL. |
| expr | A numeric type (NUMBER, FLOAT, BINARY_FLOAT, and BINARY_DOUBLE) or an expression that can be converted to a numeric type. |
| OVER | Use the OVER clause to define the window for calculation. For more information, see Analytic Function Description. |
Return type
The return type is the same as the data type of the expr parameter.
Examples
Assume that the employees table has been created.
obclient> SELECT * FROM employees;
+---------------+-----------+------------+--------+
| DEPARTMENT_ID | LAST_NAME | HIREDATE | SALARY |
+---------------+-----------+------------+--------+
| 30 | Raphaely | 2017-07-01 | 1700 |
| 30 | De Haan | 2018-05-01 | 11000 |
| 40 | Errazuriz | 2017-07-21 | 1400 |
| 50 | Hartstein | 2019-10-05 | 14000 |
| 50 | Raphaely | 2017-07-22 | 1700 |
| 50 | Weiss | 2019-10-05 | 13500 |
| 90 | Russell | 2019-07-11 | 13000 |
| 90 | Partners | 2018-12-01 | 14000 |
+---------------+-----------+------------+--------+
8 rows in set
Aggregate function example
Calculate the standard deviation of the salary column.
obclient> SELECT STDDEV_SAMP(salary) FROM employees;
+-------------------------------------------+
| STDDEV_SAMP(SALARY) |
+-------------------------------------------+
| 6026.474330580265330900400184969999384459 |
+-------------------------------------------+
1 row in set
Analytic function example
Group by the department_id column and sort the hiredate column in ascending order, then calculate the standard deviation of the salary column.
obclient> SELECT department_id, last_name, hiredate, salary,
STDDEV_SAMP(salary) OVER (PARTITION BY department_id
ORDER BY hiredate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS s_samp
FROM employees;
+---------------+-----------+------------+--------+-------------------------------------------+
| DEPARTMENT_ID | LAST_NAME | HIREDATE | SALARY | S_SAMP |
+---------------+-----------+------------+--------+-------------------------------------------+
| 30 | Raphaely | 2017-07-01 | 1700 | NULL |
| 30 | De Haan | 2018-05-01 | 11000 | 6576.093065034891976927852567575096065349 |
| 40 | Errazuriz | 2017-07-21 | 1400 | NULL |
| 50 | Raphaely | 2017-07-22 | 1700 | NULL |
| 50 | Hartstein | 2019-10-05 | 14000 | 8697.413408594534550130385653889643183203 |
| 50 | Weiss | 2019-10-05 | 13500 | 6961.561127601576503543602300090640831831 |
| 90 | Partners | 2018-12-01 | 14000 | NULL |
| 90 | Russell | 2019-07-11 | 13000 | 707.106781186547524400844362104849039285 |
+---------------+-----------+------------+--------+-------------------------------------------+
8 rows in set
