I have a pre-created table data
:
id timestamp_entry
1 "2023-01-01 04:11:24 UTC"
2 "2023-01-01 04:14:55 UTC"
...
99999 "2023-01-31 23:45:59 UTC"
where timestamp_entry
has a uniform time zone "UTC" and ranges within January 2023.
I want to create a 30-minute time skeleton and count how many entries in timestamp_entry
falls into each interval.
I first created a subquery:
WITH
intervals AS(
SELECT interval AS start_time,
TIMESTAMP_SUB(TIMESTAMP_ADD(interval, INTERVAL 30 MINUTE), INTERVAL 1 SECOND) AS end_time
FROM UNNEST(GENERATE_TIMESTAMP_ARRAY("2023-01-01 00:00:00 UTC", "2023-01-31 23:59:59 UTC", INTERVAL 30 MINUTE)) interval
)
But ideally, I want my outcome to show:
start_time end_time count
"2023-01-01 00:00:00 UTC" "2023-01-01 00:29:59 UTC" 0
"2023-01-01 00:30:00 UTC" "2023-01-01 00:59:59 UTC" 0
...
"2023-01-31 23:00:00 UTC" "2023-01-31 23:29:59 UTC" 12
"2023-01-31 23:30:00 UTC" "2023-01-31 23:59:59 UTC" 5
where count
shows how many timestamp_entry
from data
falls into each interval.
I have tried using RIGHT JOIN
with BETWEEN
, but I won't be able to join the two tables as there are no exact "matches".
Any insights are appreciated.