STDDEV_SAMP
函数计算样本标准差。STDDEV_SAMP
函数将数值型数据作为参数,返回数值型数据。它与函数 [STDDEV](https://www.oceanbase.com/docs/apsaradb-for-oceanbase/docs/V2.2.30/lbfuea)
的不同之处在于,STDDEV
只有一行输入数据时返回 0,而 STDDEV_SAMP
返回 NULL。
说明
样本标准差是样本方差的算术平方根。
语法
STDDEV_SAMP([ALL] expr) [ OVER (analytic_clause) ]
作为分析函数使用时,您需要使用窗口函数的完整语法,它对一组行的集合进行计算并返回多个值 。作为聚合函数使用时,该函数对一组行的集合进行聚合计算,结果只能返回一个值,此时不需要加 OVER 关键字。
参数
参数 | 说明 |
---|---|
ALL | 全部数值列。 |
expr | 是数值类型( |
OVER | 使用 |
返回类型
返回类型与参数 expr
的数据类型相同。
示例
分析函数示例
以下语句创建了表 employees,并向里面插入数据:
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.127178203498995615489088200868644482 |
| 100 | Errazuriz | 2017-07-21 | 1400 | 212.132034355964257320253308631454711785 |
| 100 | Hartstein | 2019-05-01 | 14000 | 6340.346993658943269176828928801701088079 |
| 100 | Partners | 2018-12-01 | 14000 | 6064.899009876421676804205219406952308814 |
| 100 | Raphaely | 2017-07-01 | 1700 | NULL |
| 100 | Raphaely | 2017-07-22 | 1700 | 173.205080756887729352744634150587236694 |
| 100 | Russell | 2019-10-05 | 13000 | 6026.474330580265330900400184969999384459 |
| 100 | Weiss | 2019-07-11 | 13500 | 6244.311697171159907069428668980211861012 |
| 200 | Bell | 2019-05-25 | 13000 | 707.106781186547524400844362104849039285 |
| 200 | Part | 2018-08-11 | 14000 | NULL |
| 200 | Ross | 2019-06-11 | 13500 | 500 |
+------------+-----------+------------+--------+-------------------------------------------+
聚合函数示例
调用函数并执行以下语句:
SELECT STDDEV_SAMP(salary) FROM employees ;
查询结果如下:
+-------------------------------------------+
| STDDEV_SAMP(SALARY) |
+-------------------------------------------+
| 5506.194858355615640082358245403620332764 |
+-------------------------------------------+