博客
关于我
Postgresql distinct/ distinct on
阅读量:392 次
发布时间:2019-03-05

本文共 1982 字,大约阅读时间需要 6 分钟。

PostgreSQL 中的 DISTINCT 和 DISTINCT ON

在 PostgreSQL 中,DISTINCTDISTINCT ON 是用来消除重复记录的重要工具。理解它们的区别和用法对于优化查询和处理数据是至关重要的。本文将详细解析这两种功能,并通过实例展示其应用。


NULL 在 DISTINCT 中的处理

在 PostgreSQL 中,NULL 值在 DISTINCT 中被特殊对待。具体来说,NULL 被视为与其他 NULL 相等的值。例如:

postgres=# create table t3 (id int);postgres=# insert into t3 values (1);postgres=# insert into t3 values (2);postgres=# insert into t3 values (null);postgres=# insert into t3 values (null);postgres=# insert into t3 values (null);postgres=# select distinct id from t3;

执行上述命令会发现,尽管插入了三个 NULLselect distinct id 的结果中只包含两个记录。这是因为 NULL 被视为相同的值,根据 DISTINCT 的规则,所有相同的记录只保留一条。


DISTINCT ON 的用法

DISTINCT ON 的功能比 DISTINCT 更强,它允许你根据指定的表达式对记录进行分组,并只保留每组的第一条记录。它的基本形式如下:

select distinct on (expression) column1, column2, ... from table;

工作原理

  • 表达式的评估:对于每一条记录,expression 被计算。
  • 分组:所有具有相同 expression 值的记录被分组。
  • 保留第一条记录:每个分组中只保留第一条记录。
  • 需要注意的是,如果没有使用 ORDER BY 子句,返回的第一条记录是不可预测的。为了确保结果的确定性,最好在 DISTINCT ON 中使用 ORDER BY 子句,并确保排序列在 expression 的左边。


    DISTINCT ON 的实际应用示例

    以下是一个典型的应用场景:从一个名单中提取每个人在各个科目上的最高成绩。

    postgres=# CREATE TABLE score_ranking (id int, name text, subject text, score numeric);postgres=# INSERT INTO score_ranking VALUES     (1,'killerbee','数学',99.5),     (2,'killerbee','语文',89.5),    (3,'killerbee','英语',79.5),     (4,'killerbee','物理',99.5),     (5,'killerbee','化学',98.5),    (6,'刘德华','数学',89.5),     (7,'刘德华','语文',99.5),     (8,'刘德华','英语',79.5),    (9,'刘德华','物理',89.5),     (10,'刘德华','化学',69.5),    (11,'张学友','数学',89.5),     (12,'张学友','语文',91.5),     (13,'张学友','英语',92.5),     (14,'张学友','物理',93.5),     (15,'张学友','化学',94.5);postgres=# select distinct on (subject) id, name, subject, score from score_ranking order by subject, score desc;

    执行上述查询后,你将获得每科的最高分记录:

    id name subject score
    5 killerbee 化学 98.5
    1 killerbee 数学 99.5
    4 killerbee 物理 99.5
    13 张学友 英语 92.5
    7 刘德华 语文 99.5

    注意事项

  • 排序依赖:如果没有使用 ORDER BY,结果的顺序是不可预测的。
  • 表达式的顺序:在 ORDER BY 子句中,排序列必须出现在 DISTINCT ON 表达式的左边。
  • 多列支持DISTINCT ON 支持多个表达式,如 distinct on (id, subject)
  • 通过合理运用 DISTINCT ON,你可以显著简化数据处理任务,并提高查询性能。

    转载地址:http://rbowz.baihongyu.com/

    你可能感兴趣的文章
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    node exporter完整版
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>