选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0
历史版本PostgreSQL 9.0 已于 2015 年 10 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本

9.18. 聚合函数 #

聚合函数从一组输入值计算单个结果。内置的聚合函数列于表 9.43表 9.44中。 聚合函数的特殊语法注意事项在第 4.2.7 节中说明。更多入门信息请参见第 2.7 节

表 9.43. 通用聚合函数

Function Argument Type(s) Return Type 描述
array_agg(expression) 任意非数组类型 参数类型的数组 将输入值(包括 null)串接为数组
avg(expression) smallint, int, bigint, real, double precision, numeric, or interval 整数类型参数为numeric,浮点参数为double precision,否则与参数数据类型相同 所有输入值的平均值(算术平均)
bit_and(expression) smallintintbigintbit 与参数数据类型相同 所有非空输入值的按位与;没有非空输入时为 null
bit_or(expression) smallintintbigintbit 与参数数据类型相同 所有非空输入值的按位或;没有非空输入时为 null
bool_and(expression) bool bool 所有输入值都为真时返回真,否则返回假
bool_or(expression) bool bool 至少一个输入值为真时返回真,否则返回假
count(*) 任意 bigint number of input rows
count(expression) 任意 bigint expression 的值不为 null 的输入行数
every(expression) bool bool 等价于 bool_and
max(expression) any array, numeric, string, or date/time type same as argument type maximum value of expression across all input values
min(expression) any array, numeric, string, or date/time type same as argument type minimum value of expression across all input values
string_agg(expression, delimiter) text, text text 将非空输入值串接为字符串,以分隔符分隔
sum(expression) smallint, int, bigint, real, double precision, numeric, interval, or money smallintint参数为bigintbigint参数为numeric,否则与参数数据类型相同 sum of expression across all input values
xmlagg(expression) xml xml 串接非空 XML 值(另见第 9.14.1.7 节

需要注意,除了count之外,这些函数在没有选中任何行时都会返回空值。特别地,sum在没有输入行时返回空值,而不是预期中的零;array_agg在没有输入行时返回空值,而不是空数组。必要时,可以用coalesce函数把空值替换成零或空数组。

注意

布尔聚合bool_andbool_or对应于标准 SQL 聚合everyanysome。至于anysome,标准语法似乎存在歧义:

SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;

此处的ANY既可以被视为引入一个子查询,也可以在该子查询返回一行布尔值时被视为聚合函数。因此,不能将标准名称用于这些聚合。

注意

习惯于其他 SQL 数据库管理系统的用户,可能会对count聚合用于整个表时的性能感到失望。如下查询:

SELECT count(*) FROM sometable;

将被PostgreSQL以顺序扫描整个表的方式执行。

聚合函数array_aggstring_aggxmlagg,以及类似的用户定义聚合函数,其结果值会随输入值的顺序发生实质性变化。默认情况下,输入顺序未指定,但可以在聚合调用中写入ORDER BY子句来控制,如第 4.2.7 节所示。也可以用已排序的子查询提供输入值,这通常也能奏效。例如:

SELECT xmlagg(x) FROM (SELECT x FROM test ORDER BY y DESC) AS tab;

But this syntax is not allowed in the SQL standard, and is not portable to other database systems.

表 9.44列出了统计分析常用的聚合函数。(将它们单独列出只是为了避免让更常用的聚合函数列表过于拥挤。)说明中提到的 N 表示所有输入表达式均非空的输入行数。在所有情况下,如果计算没有意义,例如 N 为零,都会返回空值。

表 9.44. 用于统计的聚合函数

Function Argument Type Return Type 描述
corr(Y, X) double precision double precision correlation coefficient
covar_pop(Y, X) double precision double precision population covariance
covar_samp(Y, X) double precision double precision sample covariance
regr_avgx(Y, X) double precision double precision average of the independent variable (sum(X)/N)
regr_avgy(Y, X) double precision double precision average of the dependent variable (sum(Y)/N)
regr_count(Y, X) double precision bigint number of input rows in which both expressions are nonnull
regr_intercept(Y, X) double precision double precision y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
regr_r2(Y, X) double precision double precision square of the correlation coefficient
regr_slope(Y, X) double precision double precision slope of the least-squares-fit linear equation determined by the (X, Y) pairs
regr_sxx(Y, X) double precision double precision sum(X^2) - sum(X)^2/N (sum of squares of the independent variable)
regr_sxy(Y, X) double precision double precision sum(X*Y) - sum(X) * sum(Y)/N (sum of products of independent times dependent variable)
regr_syy(Y, X) double precision double precision sum(Y^2) - sum(Y)^2/N (sum of squares of the dependent variable)
stddev(expression) smallint, int, bigint, real, double precision, or numeric double precision for floating-point arguments, otherwise numeric historical alias for stddev_samp
stddev_pop(expression) smallint, int, bigint, real, double precision, or numeric double precision for floating-point arguments, otherwise numeric population standard deviation of the input values
stddev_samp(expression) smallint, int, bigint, real, double precision, or numeric double precision for floating-point arguments, otherwise numeric sample standard deviation of the input values
variance(expression) smallintintbigintrealdouble precisionnumeric 浮点参数返回 double precision,其他情况返回 numeric var_samp 的历史别名
var_pop(expression) smallint, int, bigint, real, double precision, or numeric double precision for floating-point arguments, otherwise numeric population variance of the input values (square of the population standard deviation)
var_samp(expression) smallint, int, bigint, real, double precision, or numeric double precision for floating-point arguments, otherwise numeric sample variance of the input values (square of the sample standard deviation)

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。