博客
关于我
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/

    你可能感兴趣的文章
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>