Skip to content

Instantly share code, notes, and snippets.

@rubengmurray
Last active March 18, 2023 11:57
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment