Imaging a query that delivers "posts" in a specific time period between "from" and "to". A simple query.
Additionally, I need to get ONE record BEFORE and ONE record AFTER what the original query specified.
For example, a table holds these posts:
1 hello 20.08.2023
2 hi 21.08.2023
3 please 22.08.2023
4 bye 23.08.2023
For get_posts("from" => '21.08.2023', to => '22.08.2023')
the origin query delivers:
2 hi
3 please
Now I need to add one row before (1 hello
) and one row after (4 bye
). Possibly as subquery inside the original query?
I read about window functions or subqueries, but I do not know how to start.
2
Answers
You can also make the query shorter by using lag and lead functions instead of subqueries.
Assuming current Postgres.
Typically, you don’t need an outer
ORDER BY
with this. But consider:Unless the table is trivially small, you need an index on
posts(time_stamp)
to make this fast.Beware of
BETWEEN
when working with timestamp types. What you show looks likedate
, buttimestamptz
would make a lot more sense for the use case.Always use ISO format with date and time literals, which is unambiguous with any locale and
datestyle
setting. The meaning of'20.08.2023'
depends on settings of the current session and breaks easily.Related: