首页 > 解决方案 > 在特定列 postgres 中选择具有最大值的组中的行

问题描述

我有一个看起来像这样的数据库表。

 id       account_id      action             time_point

 3        234            delete                100
 1        656            create                600
 1        4435           update                900
 3        645            create                50

我需要按 id 对该表进行分组,并选择 time_point 具有最大值的特定行。

结果表应如下所示:

 id       account_id      action             time_point

 3        234            delete                100
 1        4435           update                900

谢谢你的帮助,qwew

标签: sqlpostgresqlsql-order-bygreatest-n-per-groupwindow-functions

解决方案


检查这个。

select * from x
where exists (
  select 1 from x xin
  where xin.id = x.id 
  having max(time_point) = time_point
);

推荐阅读