【问题标题】:Select last Modified data record from the Table从表中选择最后修改的数据记录
【发布时间】:2021-09-30 09:43:53
【问题描述】:

这是桌子的样子。我只想选择 Last Modified Date 为 Max 的记录。 EX:只会选择上表中的第二条记录。 有可能吗?

【问题讨论】:

标签: sql postgresql greatest-n-per-group


【解决方案1】:

如果即使最大值出现多次,您也只想要一行,请使用 LIMIT:

select amount, created_date, last_mod_date
from the_table
order by last_mod_date desc
limit 1;

如果最大值出现不止一次,你想要多行,你可以使用窗口函数:

select amount, created_date, last_mod_date
from (
    select amount, created_date, last_mod_date, 
           dense_rank() over (order by last_mod_date desc) as rn
    from the_table
) t 
where rn = 1;

【讨论】:

    【解决方案2】:

    使用 order by 和 limit

    select a.* from table_name a
    order by last_mod_date desc
    limit 1
    

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 2016-10-08
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多