Skip to content

Instantly share code, notes, and snippets.

@rubengmurray
Last active July 7, 2024 14:25
Show Gist options
  • Save rubengmurray/5d1d76e3b791c29ceba1b2acbe7e6fbc to your computer and use it in GitHub Desktop.
Save rubengmurray/5d1d76e3b791c29ceba1b2acbe7e6fbc to your computer and use it in GitHub Desktop.
-- 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