pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。
聚集函数从一组输入值计算单一结果。内建聚集函数列在 表 9.47 和 表 9.48 中。 聚集函数的特殊语法考虑在 第 4.2.7 节 中说明。 更多介绍信息请参阅 第 2.7 节。
表 9.47. General-Purpose 聚集函数
| 函数 | 参数类型 | 返回类型 | 描述 |
|---|---|---|---|
array_agg( |
any | 参数类型的数组 | 把输入值(包括空值)连接成数组 |
avg( |
smallint, int, bigint, real, double precision, numeric, or interval |
numeric for any integer-type argument, double precision for a floating-point argument, otherwise the same as the argument data type |
所有输入值的平均值(算术平均) |
bit_and( |
smallint, int, bigint, or bit |
与参数数据类型相同 | 所有非空输入值的按位与,如果没有非空值则结果为空值 |
bit_or( |
smallint, int, bigint, or bit |
与参数数据类型相同 | 所有非空输入值的按位或,如果没有非空值则结果为空值 |
bool_and( |
bool |
bool |
如果所有输入值都为真则为真,否则为假 |
bool_or( |
bool |
bool |
如果至少有一个输入值为真则为真,否则为假 |
count(*) |
bigint |
输入行数 | |
count( |
any | bigint |
输入行数 for which the value of expression is not null |
every( |
bool |
bool |
equivalent to bool_and |
json_agg( |
any |
json |
把值聚集为 JSON 数组 |
max( |
任意数组、数字、字符串或日期/时间类型 | 与参数类型相同 | maximum value of expression across all input values |
min( |
任意数组、数字、字符串或日期/时间类型 | 与参数类型相同 | minimum value of expression across all input values |
string_agg( |
(text, text) or (bytea, bytea) |
与参数类型相同s | 把输入值连接成字符串,用分隔符分隔 |
sum( |
smallint, int, bigint, real, double precision, numeric, interval, or money |
bigint for smallint or int arguments, numeric for bigint arguments, otherwise the same as the argument data type |
sum of expression across all input values |
xmlagg( |
xml |
xml |
concatenation of XML values (see also 第 9.14.1.7 节) |
It should be noted that except for count, these functions return a null value when no rows are selected. In particular, sum of no rows returns null, not zero as one might expect, and array_agg returns null rather than an empty array when there are no input rows. The coalesce function can be used to substitute zero or an empty array for null when necessary.
Boolean aggregates bool_and and bool_or correspond to standard SQL aggregates every and any or some. As for any and some, it seems that there is an ambiguity built into the standard syntax:
SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;
Here ANY can be considered either as introducing a subquery, or as being an aggregate function, if the subquery returns one row with a Boolean value. Thus the standard name cannot be given to these aggregates.
习惯于其他 SQL 数据库管理系统的用户可能会对 count 聚集 应用于整个表时的性能感到失望。类似下面的查询:
SELECT count(*) FROM sometable;
将需要与表大小成正比的工作量:PostgreSQL 需要扫描 整个表,或者扫描包含表中所有行的索引的全部。
The aggregate functions array_agg, json_agg, string_agg, and xmlagg, as well as similar user-defined aggregate functions, produce meaningfully different result values depending on the order of the input values. This ordering is unspecified by default, but can be controlled by writing an ORDER BY clause within the aggregate call, as shown in 第 4.2.7 节. Alternatively, supplying the input values from a sorted subquery will usually work. For example:
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.48 shows aggregate functions typically used in statistical analysis. (These are separated out merely to avoid cluttering the listing of more-commonly-used aggregates.) Where the description mentions N, it means the 输入行数 for which all the input expressions are non-null. In all cases, null is returned if the computation is meaningless, for example when N is zero.
表 9.48. 聚集函数 for Statistics
译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。