全部产品
云市场

STDDEV_POP

更新时间:2020-03-23 20:04:38

STDDEV_POP 函数计算总体标准差。STDDEV_POP 函数将数值型数据作为参数,返回数值型数据。

注意: 总体标准差是总体方差的算术平方根。

语法

  1. STDDEV_POP(expr) [ OVER (analytic_clause) ]

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

参数

参数 说明
OVER 使用 OVER 子句定义窗口进行计算。
expr 是数值类型(NUMBERFLOATBINARY_FLOATBINARY_DOUBLE)的表达式。

返回类型

返回类型与参数 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, salary, STDDEV_POP(salary) OVER (PARTITION BY manager_id) AS pop_std
  2. FROM employees ORDER BY manager_id, last_name, salary, pop_std;

查询结果如下:

  1. +------------+-----------+--------+-------------------------------------------+
  2. | MANAGER_ID | LAST_NAME | SALARY | POP_STD |
  3. +------------+-----------+--------+-------------------------------------------+
  4. | 100 | De Haan | 11000 | 5637.250548804798333699350384281939588505 |
  5. | 100 | Errazuriz | 1400 | 5637.250548804798333699350384281939588505 |
  6. | 100 | Hartstein | 14000 | 5637.250548804798333699350384281939588505 |
  7. | 100 | Partners | 14000 | 5637.250548804798333699350384281939588505 |
  8. | 100 | Raphaely | 1700 | 5637.250548804798333699350384281939588505 |
  9. | 100 | Raphaely | 1700 | 5637.250548804798333699350384281939588505 |
  10. | 100 | Russell | 13000 | 5637.250548804798333699350384281939588505 |
  11. | 100 | Weiss | 13500 | 5637.250548804798333699350384281939588505 |
  12. | 200 | Bell | 13000 | 408.248290463863016366214012450981899069 |
  13. | 200 | Part | 14000 | 408.248290463863016366214012450981899069 |
  14. | 200 | Ross | 13500 | 408.248290463863016366214012450981899069 |
  15. +------------+-----------+--------+-------------------------------------------+

聚合函数示例

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

  1. SELECT STDDEV_POP(salary) FROM employees ;

查询结果如下:

  1. +-------------------------------------------+
  2. | STDDEV_POP(SALARY) |
  3. +-------------------------------------------+
  4. | 5249.950806538512715446505486136315088416 |
  5. +-------------------------------------------+