Last active
July 7, 2024 14:25
-
-
Save rubengmurray/5d1d76e3b791c29ceba1b2acbe7e6fbc to your computer and use it in GitHub Desktop.
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
-- COUNT ITEMS BY DATE INTERVAL | |
SELECT | |
DATE_TRUNC('hour', my_date_field), -- or second, minute, hour, day, week, month, year | |
COUNT(id) | |
FROM my_table | |
WHERE my_field IS NOT NULL | |
GROUP BY 1 | |
ORDER BY 2 DESC | |
-- OUT OF SYNC PRIMARY KEY SEQUENCE | |
-- Get the next PK in existing sequence | |
SELECT nextval('PK_index_name'); | |
-- Re-synchronise the PK to the next id | |
SELECT setval('PK_index_name', COALESCE((SELECT MAX(PK_field_name)+1 FROM table_name), 1), false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment