I have a table like this:
id | created_at
--------------------------------------+-------------------------------
50d55ed2-d238-46dc-a9e4-f686d727f592 | 2021-04-27 04:16:18.744207+00
73a6cec8-6747-4713-b5d2-a098e6b022a6 | 2021-04-27 04:17:17.159264+00
a43a5f78-a30a-4597-a571-22b4bd94d71a | 2021-04-27 04:18:38.657008+00
d822ce7e-e02a-4242-8502-f0d8f5c68819 | 2021-04-27 04:20:05.74405+00
3312f868-39c8-414a-8173-68a7105ebefa | 2021-04-27 04:22:38.312941+00
7980d464-e0d6-4697-9985-c57dff863fb6 | 2021-04-27 04:24:36.473433+00
I want to find all the records that have created_at at around the time d822ce7e-e02a-4242-8502-f0d8f5c68819 was created (or, more than one id). My Postgres query is like this:
SELECT id, created_at FROM sometable1
WHERE created_at BETWEEN (
SELECT created_at - INTERVAL '2 min' as after from sometable1 WHERE id IN ('d822ce7e-e02a-4242-8502-f0d8f5c68819')
) AND (
SELECT created_at + INTERVAL '2 min' as after from sometable1 WHERE id IN ('d822ce7e-e02a-4242-8502-f0d8f5c68819')
);
The result:
id | created_at
--------------------------------------+-------------------------------
a43a5f78-a30a-4597-a571-22b4bd94d71a | 2021-04-27 04:18:38.657008+00
d822ce7e-e02a-4242-8502-f0d8f5c68819 | 2021-04-27 04:20:05.74405+00
Is it kind of silly to use two select statement? Is there a more concise method?
