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 BYFROM中的子查询,可以避免这种构造,但是它通常是最方便的选择。

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的更多相关文章

  1. 【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语 ...

  2. postgresql中使用distinct去重

    select语法 [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ...

  3. PostgreSQL的查询技巧: 零除, GENERATED STORED, COUNT DISTINCT, JOIN和数组LIKE

    零除的处理 用NULLIF(col, 0)可以避免复杂的WHEN...CASE判断, 例如 ROUND(COUNT(view_50.amount_in)::NUMERIC / NULLIF(COUNT ...

  4. MongoDB学习笔记——聚合操作之group,distinct,count

    单独的聚合命令(group,distinct,count) 单独聚合命令 比aggregate性能低,比Map-reduce灵活度低:但是可以节省几行javascript代码,后面那句话我自己加的,哈 ...

  5. SQL之DISTINCT

    警告:不能部分使用DISTINCT. DISTINCT关键字作用于所有的列,不仅仅是跟在其后的那一列.例如,你指定SELECT DISTINCT vend_id, prod_price,除非指定的两列 ...

  6. sql distinct详解以及优化

    一.distinct简介 distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用 它来返回不重复记录的条数,而不是用它来返回不重记录的所有值.其原因是distinct只有用二重循环查询 ...

  7. 10.17小结:table.copy() 和 distinct 查询

    1. 当datatable 已存在于一个dataset中时,可以使用 ds.tables.add(dt.copy()) 来向dataset 中添加datatable; 2. 当datarow已存在于一 ...

  8. Mysql distinct、group by

    具体业务场景:根据某些字段组合去重得到所有字段结果. 遇到的error:sql_mode=only_full_group_by. 原因是mysql配置问题. distinct: distinct这个关 ...

  9. LINQ 中常用函数使用: Take TakeWhile Skip SkipWhile Reverse Distinct

    1,Take 方法 Take方法用于从一个序列的开头返回指定数量的元素. string[] names = { "郭靖", "李莫愁", "欧阳晓晓& ...

随机推荐

  1. Gateway-Worker启动失败或者启动无法正常使用的几种方法

    Workerman是一款开源高性能异步PHP socket即时通讯框架.支持高并发,超高稳定性,被广泛的用于手机app.移动通讯,微信小程序,手游服务端.网络游戏.PHP聊天室.硬件通讯.智能家居.车 ...

  2. 【OS_Windows】Win10应用商店闪退和点击Cortana搜索框闪退的解决方法

    Windows10用户遇到了打开应用商店时闪退和点击Cortana小娜搜索框闪退的问题,并且在微软社区求助,得到了一种可行的解决方法,那就是查看Network List Service(网络列表服务) ...

  3. 【Spring Cloud】Spring Cloud之Zipkin server搭建以及RabbitMQ收集,分布式服务跟踪(3)

    一.搭建步骤 1)新建Spring Boot项目,引入pom坐标 <parent> <groupId>org.springframework.boot</groupId& ...

  4. Centos系统的升级

    文章来源:https://blog.csdn.net/kikajack/article/details/79396793 系统及内核版本: [root@node5 ~]# cat /etc/redha ...

  5. lua redis接口 (在ubuntu16.04 环境下配置lua-redis开发环境)

    目前成功的lua版本是5.1, 根据网络上的资料显示 lua5.1能够支持 lua-socket 安装lua及相关软件: #安装lua5. #安装lua-socketxiangg sudo apt i ...

  6. spring-boot子模块打包的jar中去掉BOOT-INF文件夹

    1.spring-boot maven打包,一般pom.xml文件里会加 <plugin> <groupId>org.springframework.boot</grou ...

  7. 201671010418 刘佳 实验十四 团队项目评审&课程学习总结

    项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十四 团队项目评审&课程学习总结 作业学习目标 (1)掌握软件项目评审会流程: (2)反思总结课程学习内容. Start: ...

  8. 项目Beta冲刺--7/7

    项目Beta冲刺--7/7 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及 ...

  9. 表之间的数据匹配(Oracle)

    来源表1: 来源表2: 生成一个交叉表: sql: select t.course_name,max(case when t.month='200706' then 'o' else '*' end) ...

  10. 31、Python程序中的协程操作(greenlet\gevent模块)

    一.协程介绍 协程:是单线程下的并发,又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的. 对比操作系统控制线程的切换,用 ...