【问题标题】:Select a specific row from a table with duplicated entries based on one field从具有基于一个字段的重复条目的表中选择特定行
【发布时间】:2018-07-22 09:19:29
【问题描述】:

我有一个表格,其中包含以下格式的数据,但是我希望能够创建一个查询来检查参考号是否重复,并且只返回具有最新 date_issued 的条目。

ref_no          name      gender place      date_issued

xgb/358632/p    John Smith  M   London      02.08.2016

Xgb/358632/p    John Smith  M   London      14.06.2017

Rtu/638932/k    Jane Doe    F   Birmingham  04.09.2017

The result from the query should be; 

ref_no          name    gender  place       date_issued

Xgb/358632/p    John Smith  M   London      14.06.2017

Rtu/638932/k    Jane Doe    F   Birmingham  04.09.2017

是否有一个相当简单的解决方案?

【问题讨论】:

标签: postgresql


【解决方案1】:

假设日期列的类型为datetimestamp

select distinct on(ref_no) * from tablename order by refno,date desc;

这是因为distinct on 抑制了括号中表达式重复的行。

【讨论】:

    猜你喜欢
    • 2013-04-13
    • 1970-01-01
    • 2015-04-10
    • 2012-03-04
    • 2020-09-17
    • 2021-03-09
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多