聚合函数从一组输入值计算单个结果。内置的聚合函数列于表 9.37和表 9.38中。聚合函数的特殊语法注意事项在第 4.2.7 节中说明。更多入门信息请参见第 2.7 节。
表 9.37. 通用聚合函数
| 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 | 所有输入值中expression的最大值
|
min(expression) | any array, numeric, string, or date/time type | same as argument type | 所有输入值中expression的最小值
|
sum(expression) | smallint, int,
bigint, real, double
precision, numeric,
interval, or money
| smallint或int参数为bigint,bigint参数为numeric,否则与参数数据类型相同
| 所有输入值中expression的总和 |
需要注意,除了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.38列出了统计分析常用的聚合函数。(将它们单独列出只是为了避免让更常用的聚合函数列表过于拥挤。)说明中提到的 N 表示所有输入表达式均非空的输入行数。在所有情况下,如果计算没有意义,例如 N 为零,都会返回空值。
表 9.38. 用于统计的聚合函数
| 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
| 自变量的平均值(sum(X)/N) |
regr_avgy(Y, X)
| double precision
| double precision
| 因变量的平均值(sum(Y)/N) |
regr_count(Y, X)
| double precision
| bigint
| 两个表达式都非空的输入行数 |
regr_intercept(Y, X)
| double precision
| double precision
| 由(X, Y)对决定的最小二乘拟合线性方程的 y 截距 |
regr_r2(Y, X)
| double precision
| double precision
| 相关系数的平方 |
regr_slope(Y, X)
| double precision
| double precision
| 由(X,
Y)对决定的最小二乘拟合线性方程的斜率 |
regr_sxx(Y, X)
| double precision
| double precision
| sum(X^2) - sum(X)^2/N(自变量的“平方和”) |
regr_sxy(Y, X)
| double precision
| double precision
| sum(X*Y) - sum(X) * sum(Y)/N(自变量与因变量乘积的“积和”) |
regr_syy(Y, X)
| double precision
| double precision
| sum(Y^2) - sum(Y)^2/N(因变量的“平方和”) |
stddev(expression)
| smallint, int,
bigint, real, double
precision, or numeric
| double precision(浮点参数),否则numeric
| stddev_samp的历史别名 |
stddev_pop(expression)
| smallint, int,
bigint, real, double
precision, or numeric
| double precision(浮点参数),否则numeric
| 输入值的总体标准差 |
stddev_samp(expression)
| smallint, int,
bigint, real, double
precision, or numeric
| double precision(浮点参数),否则numeric
| 输入值的样本标准差 |
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(浮点参数),否则numeric
| 输入值的总体方差(总体标准差的平方) |
var_samp(expression)
| smallint, int,
bigint, real, double
precision, or numeric
| double precision(浮点参数),否则numeric
| 输入值的样本方差(样本标准差的平方) |