postgresql----聚合函数

本文详细介绍了SQL中的聚合函数,包括通用聚合函数、统计聚合函数和顺序集聚合函数等,并通过实例展示了如何使用这些函数进行数据汇总和分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

聚合函数是从一组输入中计算出一个结果的函数。

测试表

test=# \d tbl_test
          Table "public.tbl_test"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | 
 name   | character varying(32) | 
 sex    | character varying(1)  | 

test=# select * from tbl_test;
 id | name | sex 
----+------+-----
  1 | 张三 | m
  2 | 李四 | m
  3 | 王五 | f
(3 rows)

 

通用聚合函数

函数参数类型返回类型描述示例结果
array_agg(expression)任意非数组类型参数类型的数组将入参包括NULL连接成一个数组select array_agg(id) from tbl_test;{1,2,3}
array_agg(expression)任意数组类型入参数据类型

将入参数组连接成更高维度的数组,输入的数组必须是相同的维度,且不允许是空或NULL

select array_agg(array['b','c','a']); {{b,c,a}}
avg(expression) smallint, int, bigint, real, double precision, numeric, or interval整形返回numeric,浮点型返回double precision,其他和入参类型相同平均值select avg(id) from tbl_test;2.0000000000000000
bit_and(expression)smallintintbigint, or bit和入参类型相同所有非NULL输入值的按位与,如果全为NULL则返回NULLselect bit_and(id) from tbl_test;0
bit_or(expression)smallintintbigint, or bit和入参类型相同所有非NULL输入值的按位或,如果全为NULL则返回NULLselect bit_or(id) from tbl_test;3
bool_and(expression)boolbool如果输入全是true则返回true,否则为falseselect bool_or(id::bool) from tbl_test;t
bool_or(expression)boolbool如果输入至少一个true,则返回true,否则返回falseselect bool_or((id-1)::bool) from tbl_test;t
count(*) bigint输入行数select count(*) from tbl_test;3
count(expression)anybigint输入行中非NULL的行数select count(id) from tbl_test;3
every(expression)boolbool功能同bool_and  
json_agg(expression)anyjson将输入聚合成一个json数组select json_agg(id) from tbl_test;[1, 2, 3]
jsonb_agg(expression)anyjsonb将输入聚合成一个json数组select jsonb_agg(id) from tbl_test;[1, 2, 3]
json_object_agg(name,value)(any, any)json将输入组成一个key/value对的json对象select json_object_agg('a','one');{ "a" : "one" }
jsonb_object_agg(name,value)(any, any)jsonb将输入组成一个key/value对的json对象select jsonb_object_agg('a','one');{"a": "one"}
max(expression)  输入最大值

select max(id) from tbl_test;

3
min(expression)  输入最小值select min(id) from tbl_test;1
string_agg(expression,delimiter)(texttext) or (byteabytea)同参数类型将输入使用delimiter连接成一个textselect string_agg(name,',') from tbl_test; 张三,李四,王五
sum(expression)smallintintbigintrealdouble precisionnumericinterval, or money 输入和select sum(id) from tbl_test;6
xmlagg(expression)xmlxml 请参考xml类型及其函数 

 

修改表

test=# alter table tbl_test add column id1 int default 1;
ALTER TABLE
test=# select * from tbl_test;
 id | name | sex | id1 
----+------+-----+-----
  1 | 张三 | m   |   1
  2 | 李四 | m   |   1
  3 | 王五 | f   |   1
(3 rows)

 

统计聚合函数

函数参数类型返回类型描述示例结果
corr(YX)double precisiondouble precision相关系数select corr(id,id) from tbl_test;1
covar_pop(YX)double precisiondouble precision总体协方差select covar_pop(id,id) from tbl_test;0.666666666666667
covar_samp(YX)double precisiondouble precision样本协方差select covar_samp(id,id1) from tbl_test;0
regr_avgx(YX)double precisiondouble precision自变量平均值(sum(X)/N)select regr_avgx(id,id1) from tbl_test;1
regr_avgy(YX)double precisiondouble precision因变量平均值(sum(Y)/N)select regr_avgy(id,id1) from tbl_test;2
regr_count(YX)double precisionbigint两个参数都不为NULL的行数 select regr_count(id,id1) from tbl_test;3
regr_intercept(YX)double precisiondouble precision根据所有输入点(X,Y)利用最小二乘法计算一个线性方程式。然后返回该直线的Y轴截距select regr_intercept(id,id) from tbl_test;0
regr_r2(YX)double precisiondouble precision相关系数平方select regr_r2(id,id) from tbl_test;1
regr_slope(YX)double precisiondouble precision根据所有输入点(X,Y)利用最小二乘法计算一个线性方程式。然后返回该直线的斜率select regr_slope(id,id) from tbl_test;1
regr_sxx(YX)double precisiondouble precisionsum(X^2) - sum(X)^2/N select regr_sxx(id,id) from tbl_test;2
regr_sxy(YX)double precisiondouble precisionsum(X*Y) - sum(X) * sum(Y)/N select regr_sxy(id,id) from tbl_test;2
regr_syy(YX)double precisiondouble precisionsum(Y^2) - sum(Y)^2/N select regr_syy(id,id) from tbl_test;2
stddev(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

同stddev_samp  
stddev_pop(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

总体标准差select stddev_pop(id) from tbl_test;0.81649658092772603273
stddev_samp(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

样本标准差select stddev_samp(id) from tbl_test;1.00000000000000000000
variance(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

同var_samp  
var_pop(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

总体方差select var_pop(id) from tbl_test;0.66666666666666666667
var_samp(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

样本方差select var_samp(id) from tbl_test;1.00000000000000000000
test=# insert into tbl_test values (2,'ww','f');
INSERT 0 1
test=# select * from tbl_test;
 id | name | sex | id1 
----+------+-----+-----
  1 | 张三 | m   |   1
  2 | 李四 | m   |   1
  3 | 王五 | f   |   1
  2 | ww   | f   |   1
(4 rows)

 

顺序集聚合函数

函数直接参数类型聚合参数类型返回类型描述示例结果
mode() WITHIN GROUP (ORDER BYsort_expression) 任意可排序类型同排序类型

返回最频繁的输入值(如果有

多个同样频繁的结果,则返回第一个)

select mode() within group (order by id) from tbl_test;2
percentile_cont(fraction) WITHIN GROUP (ORDER BY sort_expression)double precisiondouble precisionor interval同排序类型continuous percentile: returns a value corresponding to the specified fraction in the ordering, interpolating between adjacent input items if neededselect percentile_cont(0.25) WITHIN GROUP (ORDER BY id) from tbl_test;1.75
percentile_cont(fractions) WITHIN GROUP (ORDER BY sort_expression)double precision[]double precisionor intervalarray of sort expression's typemultiple continuous percentile: returns an array of results matching the shape of the fractionsparameter, with each non-null element replaced by the value corresponding to that percentile  
percentile_disc(fraction) WITHIN GROUP (ORDER BY sort_expression)double precisionany sortable typesame as sort expressiondiscrete percentile: returns the first input value whose position in the ordering equals or exceeds the specified fraction  
percentile_disc(fractions) WITHIN GROUP (ORDER BY sort_expression)double precision[]any sortable typearray of sort expression's typemultiple discrete percentile: returns an array of results matching the shape of the fractions parameter, with each non-null element replaced by the input value corresponding to that percentile  

转载于:https://www.cnblogs.com/alianbog/p/5674838.html

### 计算 Z-Score 的方法 在 PostgreSQL 数据库中,可以通过 SQL 查询来实现 Z-Score 的计算。Z-Score 是一种标准化技术,用于衡量某个数值距离均值的标准差数。其公式如下: \[ Z = \frac{x - \mu}{\sigma} \] 其中: - \( x \) 表示单个数据点, - \( \mu \) 表示总体平均值, - \( \sigma \) 表示标准偏差。 以下是具体的实现方式: #### 使用窗口函数计算 Z-Score 可以利用 PostgreSQL 提供的窗口函数 `AVG()` 和 `STDDEV_POP()` 来分别计算均值和标准差,并通过嵌套查询完成 Z-Score 的计算[^2]。 ```sql WITH stats AS ( SELECT value, AVG(value) OVER () AS mean_value, STDDEV_POP(value) OVER () AS std_deviation FROM your_table ) SELECT value, (value - mean_value) / NULLIF(std_deviation, 0) AS z_score FROM stats; ``` 在此代码片段中: 1. 首先创建了一个名为 `stats` 的临时视图,该视图包含了原始数据 (`value`)、整体均值 (`mean_value`) 和标准差 (`std_deviation`)。 2. 接着,在外部查询中计算了每个数据点相对于均值的偏离程度并除以标准差得到 Z-Score 值。这里使用了 `NULLIF` 函数防止分母为零的情况发生[^2]。 #### 创建自定义聚合函数 如果需要频繁执行此类操作,则可考虑构建一个专门针对此目的设计的用户定义聚合函数(UDA)[^2]: ```plpgsql CREATE OR REPLACE FUNCTION calc_zscore(float8[], float8[]) RETURNS TABLE(mean float8, stddev float8, values float8[]) AS $$ BEGIN RETURN QUERY SELECT avg(v), stddev_pop(v), array_agg(v ORDER BY i) INTO STRICT mean, stddev, values FROM unnest($1,$2) WITH ORDINALITY t(v,i); END;$$ LANGUAGE plpgsql IMMUTABLE; -- Example Usage: SELECT * FROM calc_zscore(array(SELECT value FROM mydata)); ``` 以上脚本定义了一种新的聚合形式——`calc_zscore`—它接受两组浮点型数组作为输入参数,并返回三部分结果:样本集合的整体平均值(`mean`)、标准方差(`stddev`)以及重新整理后的原始数据列表(`values`)。注意这里的例子仅展示概念验证用途的实际部署可能还需要进一步调整优化性能等方面因素的影响[^2]。 --- ### 注意事项 当处理大数据集时,应特别留意内存消耗情况因为某些复杂运算可能会占用较多资源。此外还需确保所选算法适合特定应用场景下的精度需求等问题存在差异性因此建议依据实际情况做出适当权衡取舍决定最终实施方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值