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 = { "郭靖", "李莫愁", "欧阳晓晓& ...
随机推荐
- Oracle数据库中 =:和 :=
=:应该相当于 a = :b 表明b是个绑定变量,需要执行时进行变量绑定. 变量绑定:变量绑定是指在sql语句的条件中使用变量而不是常量.比如shared pool里有两条sql语句,select * ...
- windows10安装redis
下载 github上下载最新(或者你需要的版本)的redis安装包,下载地址如下: https://github.com/microsoftarchive/redis/releases 打开点击版本号 ...
- linux虚拟机网络配置
环境:虚拟机-最小化安装 centos7 主机:win10 参考配置文件: TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=noBOOTPROTO=stat ...
- Miniconda安装 虚拟环境创建 与包管理
安装python 之前安装python包,导致了python里面的包不兼容,用管理工具卸载也下载不掉,重新安装也安装不上,没有办法只能卸掉python重装. 安装Anaconda Anaconda指的 ...
- Python pyc文件
什么是pyc文件 pyc 是由py文件经过编译后二进制文件,py文件变成pyc文件后,加载的速度有所提高,而且pyc是一种跨平台的字节码,是由python 的虚 拟机来执行的.pyc的内容,是跟pyt ...
- oVirt部署
所有前提建议: 关闭防火墙.selinux,配置hosts,计算机名使用域名 ovirt-engine部署 yum install http://resources.ovirt.org/pub/yum ...
- Linux-导入已安装的Linux系统
之前在安装过一个Linux系统,由于重装电脑需要将之前安装的系统重新移动到VMWare中,让Linux系统能够重新运行起来,这样也省去了每次重新安装系统的麻烦. 废话不多说,咱们直接看是如何做? 1) ...
- nuxt build 项目文件分析、nuxt build 发布后的资源如何部署cdn
建议在项目发布的时候,还是将.nuxt 进行发布到生产环境,是比较稳妥的做法 出处:https://nickfu.com/p/150 nuxt build 后的前端资源都会存放在.nuxt/dist/ ...
- scala中可以执行外部命令Process
后续用到在总结 Process(s"hadoop fs -rm -r ${path}").!!
- TestAbstract
public class TestAbstract { public static void main(String[] args) { System.out.println("Hello ...