STDDEV_SAMP 函数计算样本标准差。STDDEV_SAMP 函数将数值型数据作为参数,返回数值型数据。它与函数 [STDDEV](https://www.oceanbase.com/docs/apsaradb-for-oceanbase/docs/V2.2.30/lbfuea) 的不同之处在于,STDDEV 只有一行输入数据时返回 0,而 STDDEV_SAMP 返回 NULL。

说明

样本标准差是样本方差的算术平方根。

语法

  1. STDDEV_SAMP([ALL] expr) [ OVER (analytic_clause) ]

作为分析函数使用时,您需要使用窗口函数的完整语法,它对一组行的集合进行计算并返回多个值 。作为聚合函数使用时,该函数对一组行的集合进行聚合计算,结果只能返回一个值,此时不需要加 OVER 关键字。

参数

参数

说明

ALL

全部数值列。

expr

是数值类型(NUMBERFLOATBINARY_FLOATBINARY_DOUBLE)或者可以转换成数值类型的表达式。

OVER

使用 OVER 子句定义窗口进行计算。

返回类型

返回类型与参数 expr 的数据类型相同。

示例

分析函数示例

以下语句创建了表 employees,并向里面插入数据:

  1. CREATE TABLE employees (manager_id INT,last_name varchar(50),hiredate varchar(50),SALARY INT);
  2. INSERT INTO employees VALUES(100, 'Raphaely', '2017-07-01', 1700);
  3. INSERT INTO employees VALUES(100, 'De Haan', '2018-05-01',11000);
  4. INSERT INTO employees VALUES(100, 'Errazuriz', '2017-07-21', 1400);
  5. INSERT INTO employees VALUES(100, 'Hartstein', '2019-05-01',14000);
  6. INSERT INTO employees VALUES(100, 'Raphaely', '2017-07-22', 1700);
  7. INSERT INTO employees VALUES(100, 'Weiss', '2019-07-11',13500);
  8. INSERT INTO employees VALUES(100, 'Russell', '2019-10-05', 13000);
  9. INSERT INTO employees VALUES(100, 'Partners', '2018-12-01',14000);
  10. INSERT INTO employees VALUES(200, 'Ross', '2019-06-11',13500);
  11. INSERT INTO employees VALUES(200, 'Bell', '2019-05-25', 13000);
  12. INSERT INTO employees VALUES(200, 'Part', '2018-08-11',14000);
  13. COMMIT;

调用函数并执行以下语句:

  1. SELECT manager_id, last_name, hiredate, salary,STDDEV_SAMP(salary) OVER (PARTITION BY manager_id
  2. ORDER BY hiredate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cum_sdev
  3. FROM employees ORDER BY manager_id, last_name, hiredate, salary, cum_sdev;

返回结果如下:

  1. +------------+-----------+------------+--------+-------------------------------------------+
  2. | MANAGER_ID | LAST_NAME | HIREDATE | SALARY | CUM_SDEV |
  3. +------------+-----------+------------+--------+-------------------------------------------+
  4. | 100 | De Haan | 2018-05-01 | 11000 | 4702.127178203498995615489088200868644482 |
  5. | 100 | Errazuriz | 2017-07-21 | 1400 | 212.132034355964257320253308631454711785 |
  6. | 100 | Hartstein | 2019-05-01 | 14000 | 6340.346993658943269176828928801701088079 |
  7. | 100 | Partners | 2018-12-01 | 14000 | 6064.899009876421676804205219406952308814 |
  8. | 100 | Raphaely | 2017-07-01 | 1700 | NULL |
  9. | 100 | Raphaely | 2017-07-22 | 1700 | 173.205080756887729352744634150587236694 |
  10. | 100 | Russell | 2019-10-05 | 13000 | 6026.474330580265330900400184969999384459 |
  11. | 100 | Weiss | 2019-07-11 | 13500 | 6244.311697171159907069428668980211861012 |
  12. | 200 | Bell | 2019-05-25 | 13000 | 707.106781186547524400844362104849039285 |
  13. | 200 | Part | 2018-08-11 | 14000 | NULL |
  14. | 200 | Ross | 2019-06-11 | 13500 | 500 |
  15. +------------+-----------+------------+--------+-------------------------------------------+

聚合函数示例

调用函数并执行以下语句:

  1. SELECT STDDEV_SAMP(salary) FROM employees ;

查询结果如下:

  1. +-------------------------------------------+
  2. | STDDEV_SAMP(SALARY) |
  3. +-------------------------------------------+
  4. | 5506.194858355615640082358245403620332764 |
  5. +-------------------------------------------+