PostgreSQL DISTINCT 和 DISTINCT ON
select语句中,使用distinct关键字,在处理select list后,结果表可以选择消除重复的行。在SELECT之后直接写入DISTINCT关键字以指定此关键字:
SELECT DISTINCT select_list ...
(可以使用关键字ALL代替DISTINCT来指定保留所有行的默认行为)
显然,如果两行至少有一个列值不同,则认为它们是不同的。在此比较中,将空值视为相等。
另外,一个任意表达式可以确定哪些行被认为是不同的:
SELECT DISTINCT ON (expression [, expression ...]) select_list ...
这里的expression是一个针对所有行求值的任意值表达式。 一组所有表达式均相等的行被视为重复行,并且仅该集合的第一行保留在输出中。请注意,除非查询在足够的列上排序以保证到达DISTINCT过滤器的行的唯一顺序,否则集合的“第一行”是不可预测的。(DISTINCT ON处理在ORDER BY排序之后进行)
DISTINCT ON子句不是SQL标准的一部分,有时由于其结果的不确定性而有时被认为是不良样式。通过明智地使用GROUP BY和FROM中的子查询,可以避免这种构造,但是它通常是最方便的选择。
create table t_distinct(a int ,b int ,c int);
insert into t_distinct values(1,2,3);
insert into t_distinct values(2,3,4);
insert into t_distinct values(3,4,5); insert into t_distinct values(2,2,3);
insert into t_distinct values(3,3,4);
insert into t_distinct values(4,4,5); insert into t_distinct(a,b) values(5,6);
insert into t_distinct(a,b) values(5,6);
insert into t_distinct(a,b) values(6,7);
1.返回所有记录:
# select a,b,c from t_distinct;
a | b | c
---+---+---
1 | 2 | 3
2 | 3 | 4
3 | 4 | 5
2 | 2 | 3
3 | 3 | 4
4 | 4 | 5
5 | 6 |
5 | 6 |
6 | 7 |
(9 rows) # select all a,b,c from t_distinct;
a | b | c
---+---+---
1 | 2 | 3
2 | 3 | 4
3 | 4 | 5
2 | 2 | 3
3 | 3 | 4
4 | 4 | 5
5 | 6 |
5 | 6 |
6 | 7 |
(9 rows)
2.返回 a,b,c 唯一值。(这里NULL视为相等)
# select distinct a,b,c from t_distinct;
a | b | c
---+---+---
2 | 2 | 3
5 | 6 |
1 | 2 | 3
6 | 7 |
3 | 3 | 4
4 | 4 | 5
3 | 4 | 5
2 | 3 | 4
(8 rows)
3.返回a唯一的任意行
# select distinct on (a) a,b,c from t_distinct;
a | b | c
---+---+---
1 | 2 | 3
2 | 2 | 3
3 | 3 | 4
4 | 4 | 5
5 | 6 |
6 | 7 |
(6 rows)
使用窗口函数可以达到类似效果,但是可以确定返回哪行,因此也更慢一些:
# select * from (select row_number() over (partition by a) as rn, * from t_distinct) t where rn=1;
rn | a | b | c
----+---+---+---
1 | 1 | 2 | 3
1 | 2 | 2 | 3
1 | 3 | 3 | 4
1 | 4 | 4 | 5
1 | 5 | 6 |
1 | 6 | 7 |
(6 rows)
# select distinct on (a,b) a,b,c from t_distinct;
a | b | c
---+---+---
1 | 2 | 3
2 | 2 | 3
2 | 3 | 4
3 | 3 | 4
3 | 4 | 5
4 | 4 | 5
5 | 6 |
6 | 7 |
(8 rows) #这里NULL视为相等
# select distinct on (c) a,b,c from t_distinct;
a | b | c
---+---+---
1 | 2 | 3
3 | 3 | 4
3 | 4 | 5
5 | 6 |
(4 rows)
PostgreSQL DISTINCT 和 DISTINCT ON的更多相关文章
- 【PostgreSQL 】PostgreSQL 15对distinct的优化
示例表 table t_ex; c1 | c2 ----+---- 2 | B 4 | C 6 | A 2 | C 4 | B 6 | B 2 | A 4 | B 6 | C 2 | C 以下SQL语 ...
- postgresql中使用distinct去重
select语法 [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ...
- PostgreSQL的查询技巧: 零除, GENERATED STORED, COUNT DISTINCT, JOIN和数组LIKE
零除的处理 用NULLIF(col, 0)可以避免复杂的WHEN...CASE判断, 例如 ROUND(COUNT(view_50.amount_in)::NUMERIC / NULLIF(COUNT ...
- MongoDB学习笔记——聚合操作之group,distinct,count
单独的聚合命令(group,distinct,count) 单独聚合命令 比aggregate性能低,比Map-reduce灵活度低:但是可以节省几行javascript代码,后面那句话我自己加的,哈 ...
- SQL之DISTINCT
警告:不能部分使用DISTINCT. DISTINCT关键字作用于所有的列,不仅仅是跟在其后的那一列.例如,你指定SELECT DISTINCT vend_id, prod_price,除非指定的两列 ...
- sql distinct详解以及优化
一.distinct简介 distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用 它来返回不重复记录的条数,而不是用它来返回不重记录的所有值.其原因是distinct只有用二重循环查询 ...
- 10.17小结:table.copy() 和 distinct 查询
1. 当datatable 已存在于一个dataset中时,可以使用 ds.tables.add(dt.copy()) 来向dataset 中添加datatable; 2. 当datarow已存在于一 ...
- Mysql distinct、group by
具体业务场景:根据某些字段组合去重得到所有字段结果. 遇到的error:sql_mode=only_full_group_by. 原因是mysql配置问题. distinct: distinct这个关 ...
- LINQ 中常用函数使用: Take TakeWhile Skip SkipWhile Reverse Distinct
1,Take 方法 Take方法用于从一个序列的开头返回指定数量的元素. string[] names = { "郭靖", "李莫愁", "欧阳晓晓& ...
随机推荐
- k8s的node节点无法调度的问题
1.现象,创建deployment时 2.查看污点 [fedora@k8s-cluster--ycmwlao4q5wz-master- ~]$ kubectl describe node k8s-cl ...
- Golang中文乱码问题
在学习golang读取文件的过程中,遇到中文显示乱码的问题!golang没有自带的编解码包,因此需要借助第三方包 解决方法: 引入第三发转码包:git clone https://github.com ...
- VirtualDub在处理WMV文件时显示“MISSING CODEC”怎么办
以下内容主要来自:http://www.brilliantcode.com/virtualdub-is-showing-missing-codec-when-i-play-a-wmv-movie-ev ...
- Kafka Streams开发入门(3)
背景 上一篇我们介绍了Kafka Streams中的消息过滤操作filter,今天我们展示一个对消息进行转换Key的操作,依然是结合一个具体的实例展开介绍.所谓转换Key是指对流处理中每条消息的Key ...
- mysql 查询当天数据
查询当天数据 select * from tab where FROM_UNIXTIME(fabutime, '%Y%m%d') = 20121217; mysql TO_DAYS(date) 函 ...
- Linux安装在虚拟机上
虚拟机上安装centos7 minimal 详细操作链接:https://blog.csdn.net/babyxue/article/details/80970526 镜像文件 xxx.iso 本质就 ...
- 更改 Ubuntu 的 apt 源
1.在更改apt源之前要先备份官方自带的apt源 cd /etc/apt sudo cp sources.list sources.list.bak 2. 更改 sources.list 文件 sud ...
- less使用手记 主题切换 全局import less
实现主题颜色切换 components/theme.less,跟据@theme读取主题布局 @theme: dark; .dark-theme (@transparency) when (@theme ...
- 分布式中的分库分表之后,ID 主键如何处理?
面试题 分库分表之后,id 主键如何处理?(唯一性,排序等) 面试官心理分析 其实这是分库分表之后你必然要面对的一个问题,就是 id 咋生成?因为要是分成多个表之后,每个表都是从 1 开始累加,那肯定 ...
- Elasticsearch 待办
日期格式:yyyy-MM-dd,改为 yyyy-MM-dd HH:mm:ss.SSS:实体类路径:https://github.com/cag2050/spring_boot_elasticsearc ...