In PostgreSQL 10.0, I have a big table with two columns:
CREATE TABLE xs (
ts timestamp with time zone NOT NULL,
x integer NOT NULL
)
Now, if I have 100 given time points, how do I return exactly 100 x
-es that are right before (<=
) these time points (for each of those points)?
For just a single one this would be very easy:
SELECT x FROM xs WHERE ts <= '2018-03-13 11:41:47.167973+00' LIMIT 1
But how to do this efficiently for a 100? Something like:
SELECT x FROM xs
WHERE ts FIRST_LESS_THAN_EQUAL_IN
('2018-03-13 11:41:47.167973+00',
'2018-03-13 11:41:47.198564+00',
'2018-03-13 11:41:47.555668+00',
...
'2018-03-13 11:41:57.766888+00')
Obviously, there is an index on ts
.
order by
, so it can return any x wherets
is less or equal to given, not the right before.ORDER BY
!ts
is not unique, but the I can select an arbitrary value (x
s for the samets
will be the same or very close).