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. 修改pyechart生成的HTML内容大小方法

    1.目前安装pyechart后生成的图片默认大小为 width=800, height=400, 如图 2.想修改内容大小:找到C:\python\Lib\site-packages\pyechart ...

  2. Plan B

    王兴曾经说过: 2019 年是过去 10 年中最差的一年,也是未来 10 年中最好的一年. 之前我希望王兴预判错了,但现在我发现这位掌控着生活消费类数据的大佬应该不是扯淡. 今年的内部和外部环境真的很 ...

  3. HashMap中的putIfAbsent使用示例

    原文:https://blog.csdn.net/k3108001263/article/details/83720445 如果不存在key,则添加到HashMap中,跟put方法相似 如果存在key ...

  4. 使用java spring开发ckeditor的文件上传功能(转)

    说明:原帖提供的代码无法直接运行.本人在原帖基础上做了一些修改,修复了一些bug. 关于CKEditor的使用,网络上有无数的文章,这里不再赘述.而关于java支持的文件上传功能,网络上同样有千千万万 ...

  5. Type Encodings

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles ...

  6. python - 将天数转换成日期

    # 如果是 0 则为今天 def getdate(day): today = datetime.datetime.now() deviation = datetime.timedelta(days=- ...

  7. MongoDB权限配置

    参考文章:https://blog.csdn.net/qq_26896281/article/details/81206492 https://blog.csdn.net/u012373281/art ...

  8. BZOJ 4903: [Ctsc2017]吉夫特 数论+dp

    思路很巧妙的一道题 ~ 这个应该不完全是正解,复杂度约为 $O(3\times 10^8)$,有时间再研究研究正解. 首先,最裸的暴力是按照权值从小到大枚举每一个数,然后枚举后面的数来更新方案数,是 ...

  9. 常见WinDbg问题及解决方案

    当你调试一个程序时,你最不想处理的是调试器不能正常工作.当你试图集中精力跟踪一个bug时,总是会因为次要的问题而被忽略,尤其是当调试器的问题导致你失去一个重新编程或者浪费了大量的时间等待调试器完成它, ...

  10. 链表 | 递归删除不带头结点链表所有x元素

    王道P37 T1 : 设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点. 王道上的答案绝对是错的,我自己想了一个 函数主体 LinkList* del_x(LinkList* prior, ...