STDDEV_SAMP
This function calculates the cumulative sample standard deviation and returns the square root of the overall variable with the same return value as the square root of the VAR_POP function.
Example:
begin;
CREATE TABLE employees (manager_id INT,last_name varchar(50),hiredate varchar(50),SALARY INT);
INSERT INTO employees VALUES(100, 'Raphaely', '2017-07-01', 1700);
INSERT INTO employees VALUES(100, 'De Haan', '2018-05-01',11000);
INSERT INTO employees VALUES(100, 'Errazuriz', '2017-07-21', 1400);
INSERT INTO employees VALUES(100, 'Hartstein', '2019-05-01',14000);
INSERT INTO employees VALUES(100, 'Raphaely', '2017-07-22', 1700);
INSERT INTO employees VALUES(100, 'Weiss', '2019-07-11',13500);
INSERT INTO employees VALUES(100, 'Russell', '2019-10-05', 13000);
INSERT INTO employees VALUES(100, 'Partners', '2018-12-01',14000);
INSERT INTO employees VALUES(200, 'Ross', '2019-06-11',13500);
INSERT INTO employees VALUES(200, 'Bell', '2019-05-25', 13000);
INSERT INTO employees VALUES(200, 'Part', '2018-08-11',14000);
COMMIT;
SELECT manager_id, last_name, hiredate, salary,STDDEV_SAMP(salary) OVER (PARTITION BY manager_id
ORDER BY hiredate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cum_sdev FROM employees ORDER BY manager_id, last_name, hiredate, salary, cum_sdev;
MANAGER_ID | LAST_NAME | HIREDATE | SALARY | CUM_SDEV
------------+-----------+------------+--------+--------------------
100 | De Haan | 2018-05-01 | 11000 | 4702.127178203499
100 | Errazuriz | 2017-07-21 | 1400 | 212.132034355964
100 | Hartstein | 2019-05-01 | 14000 | 6340.346993658943
100 | Partners | 2018-12-01 | 14000 | 6064.899009876422
100 | Raphaely | 2017-07-01 | 1700 |
100 | Raphaely | 2017-07-22 | 1700 | 173.205080756888
100 | Russell | 2019-10-05 | 13000 | 6026.474330580265
100 | Weiss | 2019-07-11 | 13500 | 6244.31169717116
200 | Bell | 2019-05-25 | 13000 | 707.106781186548
200 | Part | 2018-08-11 | 14000 |
200 | Ross | 2019-06-11 | 13500 | 500
(11 rows)
Top comments (0)