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

测试表

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则返回NULL select bit_and(id) from tbl_test; 0
bit_or(expression) smallintintbigint, or bit 和入参类型相同 所有非NULL输入值的按位或,如果全为NULL则返回NULL select bit_or(id) from tbl_test; 3
bool_and(expression) bool bool 如果输入全是true则返回true,否则为false select bool_or(id::bool) from tbl_test; t
bool_or(expression) bool bool 如果输入至少一个true,则返回true,否则返回false select bool_or((id-1)::bool) from tbl_test; t
count(*)   bigint 输入行数 select count(*) from tbl_test; 3
count(expression) any bigint 输入行中非NULL的行数 select count(id) from tbl_test; 3
every(expression) bool bool 功能同bool_and    
json_agg(expression) any json 将输入聚合成一个json数组 select json_agg(id) from tbl_test; [1, 2, 3]
jsonb_agg(expression) any jsonb 将输入聚合成一个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连接成一个text select string_agg(name,',') from tbl_test;  张三,李四,王五
sum(expression) smallintintbigintrealdouble precisionnumericinterval, or money   输入和 select sum(id) from tbl_test; 6
xmlagg(expression) xml xml   请参考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 precision double precision 相关系数 select corr(id,id) from tbl_test; 1
covar_pop(YX) double precision double precision 总体协方差 select covar_pop(id,id) from tbl_test; 0.666666666666667
covar_samp(YX) double precision double precision 样本协方差 select covar_samp(id,id1) from tbl_test; 0
regr_avgx(YX) double precision double precision 自变量平均值(sum(X)/N) select regr_avgx(id,id1) from tbl_test; 1
regr_avgy(YX) double precision double precision 因变量平均值(sum(Y)/N) select regr_avgy(id,id1) from tbl_test; 2
regr_count(YX) double precision bigint 两个参数都不为NULL的行数  select regr_count(id,id1) from tbl_test; 3
regr_intercept(YX) double precision double precision 根据所有输入点(X,Y)利用最小二乘法计算一个线性方程式。然后返回该直线的Y轴截距 select regr_intercept(id,id) from tbl_test; 0
regr_r2(YX) double precision double precision 相关系数平方 select regr_r2(id,id) from tbl_test; 1
regr_slope(YX) double precision double precision 根据所有输入点(X,Y)利用最小二乘法计算一个线性方程式。然后返回该直线的斜率 select regr_slope(id,id) from tbl_test; 1
regr_sxx(YX) double precision double precision sum(X^2) - sum(X)^2/N  select regr_sxx(id,id) from tbl_test; 2
regr_sxy(YX) double precision double precision sum(X*Y) - sum(X) * sum(Y)/N  select regr_sxy(id,id) from tbl_test; 2
regr_syy(YX) double precision double precision sum(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 precision double precisionor interval 同排序类型 continuous percentile: returns a value corresponding to the specified fraction in the ordering, interpolating between adjacent input items if needed select 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 interval array of sort expression's type multiple 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 precision any sortable type same as sort expression discrete 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 type array of sort expression's type multiple 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    

postgresql----聚合函数的更多相关文章

  1. PostgreSQL聚合函数的filter子句

    一张表存储了学生id,科目,分数三个字段,求每个学生60分以下与参加的总科目占比.(今天电脑不好用,图片总是这样) 其实一个count(*) filter  就可以查出来,但是没用过PG的一个人竟然说 ...

  2. postgresql 自定义聚合函数

    方法1 CREATE OR REPLACE FUNCTION public.sfun_test1( results numeric[], val numeric) RETURNS numeric[] ...

  3. postgresql 所有聚合函数整理

    SELECT DISTINCT(proname) FROM pg_proc WHERE proisagg order by proname 查所有 SELECT * FROM pg_proc WHER ...

  4. MySQL数据库学习笔记(四)----MySQL聚合函数、控制流程函数(含navicat软件的介绍)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  5. MySQL聚合函数、控制流程函数(含navicat软件的介绍)

    MySQL聚合函数.控制流程函数(含navicat软件的介绍) 一.navicat的引入:(第三方可视化的客户端,方便MySQL数据库的管理和维护) NavicatTM是一套快速.可靠并价格相宜的数据 ...

  6. ORM之自关联、add、set方法、聚合函数、F、Q查询和事务

    一.外键自关联(一对多) 1.建表 # 评论表 class Comment(models.Model): id = models.AutoField(primary_key=True) content ...

  7. MySQL聚合函数、控制流程函数

    [正文] 一.navicat的引入:(第三方可视化的客户端,方便MySQL数据库的管理和维护) NavicatTM是一套快速.可靠并价格相宜的数据库管理工具,专为简化数据库的管理及降低系统管理成本而设 ...

  8. 可以这样去理解group by和聚合函数

    写在前面的话:用了好久group by,今天早上一觉醒来,突然感觉group by好陌生,总有个筋别不过来,为什么不能够select * from Table group by id,为什么一定不能是 ...

  9. TSQL 聚合函数忽略NULL值

    max,min,sum,avg聚合函数会忽略null值,但不代表聚合函数不返回null值,如果表为空表,或聚合列都是null,则返回null.count 聚合函数忽略null值,如果聚合列都是null ...

  10. SQL Server 聚合函数算法优化技巧

    Sql server聚合函数在实际工作中应对各种需求使用的还是很广泛的,对于聚合函数的优化自然也就成为了一个重点,一个程序优化的好不好直接决定了这个程序的声明周期.Sql server聚合函数对一组值 ...

随机推荐

  1. Apache Kafka学习 (二) - 多代理(broker)集群

    1. 配置server.properties > cp config/server.properties config/server-1.properties> cp config/ser ...

  2. CCNA实验3.单臂路由器

    拓扑图: 一.交换机配置 通过路由器子接口的方式实现vlan之间的路由. conf t vlan 10 vlan 20 int f0/1 switchport access vlan 10 int f ...

  3. Shell获取某目录下所有文件夹的名称

    查看目录下面的所有文件: #!/bin/bash cd /目标目录 for file in $(ls *) do echo $file done 延伸的方法,查看目录下面的所有目录 #!/bin/ba ...

  4. stingray中使用angularjs

    引入angularjs 手动启用angularjs 不使用ng-app, 在所有模块和controller定义后挂载启用angularjs function OnLoad() { scroll(0, ...

  5. Spark GraphX实例(3)

    7. 图的聚合操作 图的聚合操作主要的方法有: (1) Graph.mapReduceTriplets():该方法有一个mapFunc和一个reduceFunc,mapFunc对图中的每一个EdgeT ...

  6. Python 贝叶斯分类

    很久的时间没有更新了,一是因为每天加班到比较晚的时间,另外,公司不能上网,回家后就又懒得整理,最近在看机器学习实战的书籍,因此才又决定重新拾起原先的博客! 今天讲的是第三章的贝叶斯分类方法,我们从一个 ...

  7. selenium是如何启动浏览器的

    前几天有同学问到selenium是怎么样启动浏览器的(selenium启动浏览器的原理),当时稍微讲解了一下,不过自我感觉不够具体,现在特地把启动原理通过代码和一系列操作给串联起来,希望可以帮助大家更 ...

  8. 在Repeater中使用DropDownList的方法

    在Repeater中使用DropDownList的方法 以下代码并不完整,只记录了关键的方法 aspx代码中 假设这是一个用户管理的系统的模型,有一个下拉菜单来操作用户状态 <asp:Repea ...

  9. NodeJs相关系列文章

    1.图片上传之FileAPI与NodeJs 2.NodeJs之调试 3.CentOS下使用NVM 4.NodeJs之进程守护 5.Ubuntu下使用nvm 6.NodeJs之pm2 7.NodeJs之 ...

  10. SNF快速开发平台MVC-EasyUI3.9之-ueditor富文本编辑在 asp.net MVC下使用步骤

    mvc项目中用到了这个富文本编辑就试着把遇到的问题个使用步骤在这里记录一下,希望大家少走弯路. 1.首先我们先下载net版本的uediot 2.然后把整个文档拷贝到我们的项目中,记得是整个 把下载的文 ...