获取给定第二个Postgres的最新记录



我有毫秒精度的时间戳数据。我只想过滤给定秒内的最新时间戳。Ie.records(2020-07-13 5:05.38.009,event1(,(2020-07-13 5:05.38.012,event2(应该只检索后者。

我试过以下几种:

SELECT 
timestamp as time, event as value, event_type as metric 
FROM 
table
GROUP BY 
date_trunc('second', time)

但后来我也被要求按事件分组,我看到了所有的数据(好像没有提供分组依据(

在Postgres中,您可以使用distinct on:

select distinct on (date_trunc('second', time)) t.*
from t
order by time desc;

最新更新