Skip to content

Instantly share code, notes, and snippets.

@cabecada
Last active March 12, 2023 09:42
Show Gist options
  • Save cabecada/c67442c051404d1ff5548b40a0270398 to your computer and use it in GitHub Desktop.
Save cabecada/c67442c051404d1ff5548b40a0270398 to your computer and use it in GitHub Desktop.
grouping by bucket size postgres
https://stackoverflow.com/questions/22157718/postgres-group-by-timestamp-into-6-hourly-buckets
select count(1), trunc(extract (minute from x) / 5) from generate_series(now() - '60 minute'::interval, now(), '1 minute'::interval) x group by trunc(extract (minute from x) / 5) order by 2 desc;
count | trunc
-------+-------
5 | 11
5 | 10
5 | 9
5 | 8
5 | 7
5 | 6
5 | 5
5 | 4
5 | 3
6 | 2
5 | 1
5 | 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment