↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

支持中的版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发中的版本: 19 / 20devel
已结束支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
历史版本。 PostgreSQL 9.0 已结束支持。 2015-10-08. 请参阅 当前版本手册.

9.18. 聚合函数 #

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

表 9.43. 通用聚合函数

FunctionArgument 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) smallint、int、bigint 或 bit与参数数据类型相同所有非空输入值的按位与;没有非空输入时为 null
bit_or(expression) smallint、int、bigint 或 bit与参数数据类型相同所有非空输入值的按位或;没有非空输入时为 null
bool_and(expression) bool bool 所有输入值都为真时返回真,否则返回假
bool_or(expression) bool bool 至少一个输入值为真时返回真,否则返回假
count(*)任意bigintnumber of input rows
count(expression)任意bigintexpression 的值不为 null 的输入行数
every(expression) bool bool 等价于 bool_and
max(expression)any array, numeric, string, or date/time typesame as argument type maximum value of expression across all input values
min(expression)any array, numeric, string, or date/time typesame 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 smallint或int参数为bigint,bigint参数为numeric,否则与参数数据类型相同 sum of expression across all input values
xmlagg(expression) xml xml 串接非空 XML 值(另见第 9.14.1.7 节)

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

注意

布尔聚合bool_and和bool_or对应于标准 SQL 聚合every和any或some。至于any和some,标准语法似乎存在歧义:

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

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

注意

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

SELECT count(*) FROM sometable;

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

聚合函数array_agg、string_agg和xmlagg,以及类似的用户定义聚合函数,其结果值会随输入值的顺序发生实质性变化。默认情况下,输入顺序未指定,但可以在聚合调用中写入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. 用于统计的聚合函数

FunctionArgument TypeReturn 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) smallint、int、bigint、real、double precision 或 numeric浮点参数返回 double precision,其他情况返回 numericvar_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)

报告文档问题

阅读 上游文档. 反馈更正前请先核对 当前版本手册.