--取分组中出现频率最高的值或表达式, 如果最高频率的值有多个, 则随机取一个.
mode() WITHIN GROUP (ORDER BYsort_expression)
postgres=# create table test(id int, info text);
CREATE TABLE
postgres=# insert into test values (1,'test1');
INSERT 0 1
postgres=# insert into test values (1,'test1');
INSERT 0 1
postgres=# insert into test values (1,'test2');
INSERT 0 1
postgres=# insert into test values (1,'test3');
INSERT 0 1
postgres=# insert into test values (2,'test1');
INSERT 0 1
postgres=# insert into test values (2,'test1');
INSERT 0 1
postgres=# insert into test values (2,'test1');
INSERT 0 1
postgres=# insert into test values (3,'test4');
INSERT 0 1
postgres=# insert into test values (3,'test4');
INSERT 0 1
postgres=# insert into test values (3,'test4');
INSERT 0 1
postgres=# insert into test values (3,'test4');
INSERT 0 1
postgres=# insert into test values (3,'test4');
INSERT 0 1
postgres=# select * from test;
id | info
----+-------
1 | test1
1 | test1
1 | test2
1 | test3
2 | test1
2 | test1
2 | test1
3 | test4
3 | test4
3 | test4
3 | test4
3 | test4
(12 rows)
取出所有数据中, 出现频率最高的info, 有可能是test1也有可能是test4, 因为他们的出现频率一致.
mode的返回结果数据类型和order by后面的表达式一致.
postgres=# select mode() within group (order by info) from test;
mode
-------
test1
(1 row)
如果按INFO来分组的话, 取出出现频率最高的info, 实际上这个操作是没有任何意义的, 返回值就是所有记录的info的唯一值.
postgres=# select mode() within group (order by info) from test group by info;
mode
-------
test1
test2
test3
test4
(4 rows)
按id来分组, 取出组内出现频率最高的info值, 这个是有意义的.
postgres=# select mode() within group (order by info) from test group by id;
mode
-------
test1
test1
test4
(3 rows)
id=1 , 出现频率最高的info是test1. 出现2次.
如下 :
postgres=# select id,info,count(*) from test group by id,info;
id | info | count
----+-------+-------
1 | test1 | 2
1 | test3 | 1
3 | test4 | 5
1 | test2 | 1
2 | test1 | 3
(5 rows)
如果要返回mode()并返回频率次数. 可以使用row_number()窗口来实现. 如下.
postgres=# select id,info,cnt from (select id,info,cnt,row_number() over(partition by id order by cnt desc) as rn from (select id,info,count(*) cnt from test group by id,info) t) t where t.rn=1;
id | info | cnt
----+-------+-----
1 | test1 | 2
2 | test1 | 3
3 | test4 | 5
(3 rows)
其他, mode的返回结果数据类型和order by后面的表达式一致.
postgres=# select mode() within group (order by id) from test;
mode
------
3
(1 row)
postgres=# select mode() within group (order by id+1) from test;
mode
------
4
(1 row)
postgresql mode 函数相关推荐
- PostgreSQL日期函数备忘
PostgreSQL日期函数备忘 http://www.postgresql.org/docs/current/static/functions-formatting.html Date/Time F ...
- postgresql常用函数及操作符及类型转换
为什么80%的码农都做不了架构师?>>> 一.逻辑操作符: 常用的逻辑操作符有:AND.OR和NOT.其语义与其它编程语言中的逻辑操作符完全相同. 二.比较操作符: 下面是Po ...
- postgresql 字符串函数
字符串函数: postgresql中的字符串函数有:计算字符串长度函数.字符串合并函数.字符串替换函数.字符串比较函数.查找指定字符串位置函数等. 1.计算字符串字符数和字符串长度的函数:char_l ...
- PostgreSQL常用函数以及操作符
一.逻辑操作符: 常用的逻辑操作符有:AND.OR和NOT.其语义与其它编程语言中的逻辑操作符完全相同. 二.比较操作符: 下面是PostgreSQL中提供的比较操作符列表: 操作符 描述 < ...
- PostgreSQL之函数和操作符
一.逻辑操作符: 常用的逻辑操作符有:AND.OR和NOT.其语义与其它编程语言中的逻辑操作符完全相同. 二.比较操作符: 下面是PostgreSQL中提供的比较操作符列表: 操作符 描述 < ...
- postgresql 循环函数开发(while,for)
postgresql 循环函数开发(while,for) 上一篇文章介绍的是pg函数的开发,这篇讲循环函数的开发.在存储过程开发是很常用的. while 循环代码如: for 循环代码如:
- PostGreSQL开窗函数
PostGreSQL开窗函数 语法 <窗口函数> over(partition by 分组列 order by 排序列) order by 并非必要 over() 是开窗函数的关键词 窗口 ...
- PostgreSQL自定义函数
PostgreSQL自定义函数 函数语法 实例 动态执行语句 话不多说,下面直接拿实例说话. 函数语法 CREATE [OR REPLACE] FUNCTION function_name (argu ...
- PostgreSQL 聚合函数讲解 - 3 总体|样本 方差, 标准方差
https://yq.aliyun.com/articles/147?spm=a2c4e.11153940.blogcont148.23.1526746cjd2A4Y 摘要: PostgreSQL自带 ...
- postgresql自定义函数实现,通过contrib模块进行扩展
概要 PostgreSQL从设计上是可扩展的,从用户的角度可以通过四种方式对PostgresSQL进行扩展 query language functions (functions written in ...
最新文章
- Java项目命名规范
- 聊聊Java的泛型及实现
- 在Chrome开发者工具里观察到的SAP Spartacus productCodes
- 计算机 数据库知识点,数据库知识点总结
- mysql使用索引下推的好处_mysql的索引下推理解和实践
- Java:汇总堆外数据
- 【机房收费系统】--SSTab控件与MSHFlexGrid控件
- docker search oracle,Docker search 命令
- linux中SPI相关API函数,linux spi驱动开发学习(一)-----spi子系统架构
- PIC单片机应用开发实践教程(四): MPLAB X IDE Debug
- linux红帽子镜像下载,红帽Red Hat Linux相关产品iso镜像下载
- Android studio 三大模拟器比较
- 从电影《心灵捕手》谈起
- 09、Flutter FFI Dart Native API
- 解决Anaconda无法更新的问题
- 调用FFmpeg的视频压缩批处理脚本
- FreeCAD新手入门
- doc与docx批量互转宏代码
- 画廊php网站,美术画室画廊艺术设计网站整站源码 v5.7sp1
- obs有没有android版本,obs插件手机版下载-obs插件 安卓版v1.0.0-pc6手机下载
热门文章
- 2021年Android面试心得,已整理成文档
- docker初识_初识Docker - 阮少爷的个人空间 - OSCHINA - 中文开源技术交流社区
- KeePass密码管理软件 ---下载安装及汉化
- 为什么波长越长,衍射现象越明显?越容易传播的远?
- 武林外传-经典台词-郑昀整理版本
- 如何与人交流,如何做好演讲
- 我不想与任何人比高低
- 水中铅超标如何处理?除铅吸附材料
- 02-06 普通线性回归(波斯顿房价预测)+特征选择
- DeeCamp 2020总冠军,Mixlab社区专访 | AI+积木 = 方仔照相馆