如何解决选择组中在特定列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
解决方法
检查。
select * from x
where exists (
select 1 from x xin
where xin.id = x.id
having max(time_point) = time_point
);
,
在Postgres中,我建议使用SwDbgSrv.exe
来解决每组头1个问题:
distinct on
但是,这不允许联系。如果是这样,select distinct on (id) *
from mytable
order by id,time_point desc
是更好的解决方案:
rank()
或者,如果您正在运行Postgres 13:
select *
from (
select t.*,rank() over(partition by id order by time_point desc) rn
from mytable t
) t
where rn = 1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。