Last active
January 16, 2023 00:28
-
-
Save beeleebow/74b2855da55bb134dd77824aa9e7200a to your computer and use it in GitHub Desktop.
PSQL - Group Rows with timestamp interval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Smart idea of generating the same marker for all the timestamp within the same interval.