Skip to content
Advertisement

Getting row samples from PostgreSQL

I have a data table with timestamp and some data columns. The rows are inserted in arbitrary time intervals, i.e. the timestamp difference of consecutive rows is not stable but ranges from several seconds to several hours.

I need to select one row per time interval of constant length, if there is any.

Example

To get the time intervals for selection, I have a function that generates table of time intervals, for example:

Then for this source data:

I require result:

  • the first row out of three is selected for the first interval
  • no row is selected for the second interval
  • the first and only possible row is selected for the third interval

Thanks

Advertisement

Answer

You can use a lateral join to pick one row per interval:

(A lateral join is the first thing that comes to mind. To join all rows first and then use DISTINCT ON instead, could lead to a large intermediate result, so I would prefer the lateral join over that approach.)

Advertisement