Skip to content

Instantly share code, notes, and snippets.

@beeleebow
Last active January 16, 2023 00:28
Show Gist options
  • Save beeleebow/74b2855da55bb134dd77824aa9e7200a to your computer and use it in GitHub Desktop.
Save beeleebow/74b2855da55bb134dd77824aa9e7200a to your computer and use it in GitHub Desktop.
PSQL - Group Rows with timestamp interval
-- POSTGRES SQL.
-- Count of rows where a timestamp column falls in interval.
-- This example uses a 5 minute interval ( 300 seconds = 5 minutes).
-- You need to replace the things inside square brackets, i.e. provide
-- the name of the table and timestamp column.
SELECT COUNT(*) cnt,
to_timestamp(floor((extract('epoch' from [Timestamp Column]) / 300 )) * 300)
AT TIME ZONE 'UTC' as expiry_interval
FROM [Table] GROUP BY expiry_interval
order by expiry_interval
@FanchenBao
Copy link

Smart idea of generating the same marker for all the timestamp within the same interval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment