云数据库OceanBase入门教程 OceanBase STDDEV_POP

2024-02-26 开发教程 云数据库OceanBase入门教程 匿名 5

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

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

语法

STDDEV_POP([ALL] expr) [ OVER (analytic_clause) ]

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

参数

参数

说明

ALL

全部数值列。

OVER

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

expr

是数值类型(NUMBERFLOATBINARY_FLOATBINARY_DOUBLE)的表达式。

返回类型

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

查询结果如下:

+------------+-----------+--------+-------------------------------------------+
| MANAGER_ID | LAST_NAME | SALARY | POP_STD |
+------------+-----------+--------+-------------------------------------------+
| 100 | De Haan | 11000 | 5637.250548804798333699350384281939588505 |
| 100 | Errazuriz | 1400 | 5637.250548804798333699350384281939588505 |
| 100 | Hartstein | 14000 | 5637.250548804798333699350384281939588505 |
| 100 | Partners | 14000 | 5637.250548804798333699350384281939588505 |
| 100 | Raphaely | 1700 | 5637.250548804798333699350384281939588505 |
| 100 | Raphaely | 1700 | 5637.250548804798333699350384281939588505 |
| 100 | Russell | 13000 | 5637.250548804798333699350384281939588505 |
| 100 | Weiss | 13500 | 5637.250548804798333699350384281939588505 |
| 200 | Bell | 13000 | 408.248290463863016366214012450981899069 |
| 200 | Part | 14000 | 408.248290463863016366214012450981899069 |
| 200 | Ross | 13500 | 408.248290463863016366214012450981899069 |
+------------+-----------+--------+-------------------------------------------+

聚合函数示例

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

SELECT STDDEV_POP(salary) FROM employees ;

查询结果如下:

+-------------------------------------------+
| STDDEV_POP(SALARY) |
+-------------------------------------------+
| 5249.950806538512715446505486136315088416 |
+-------------------------------------------+