↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

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 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
历史版本PostgreSQL 8.3 已于 2013 年 2 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本。

9.18. 聚合函数 #

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

表 9.41. 通用聚合函数

Function Argument Type(s) Return Type 描述
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(*) 任意 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
sum(expression) smallint, int, bigint, real, double precision, numeric, interval, or money smallint或int参数为bigint,bigint参数为numeric,否则与参数数据类型相同 sum of expression across all input values

需要注意,除了count之外,这些函数在没有选中任何行时都会返回空值。特别地,sum在没有输入行时返回空值,而不是预期中的零。必要时,可以用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以顺序扫描整个表的方式执行。

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

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

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) smallint、int、bigint、real、double precision 或 numeric 浮点参数返回 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 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。