Skip to content

Instantly share code, notes, and snippets.

@hbasria
Forked from hgmnz/weeks-grouping.sql
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbasria/7b9387dabdfd2f963e44 to your computer and use it in GitHub Desktop.
Save hbasria/7b9387dabdfd2f963e44 to your computer and use it in GitHub Desktop.
WITH weeks AS (
SELECT distinct date_trunc('week', dates)::date AS start,
daterange(date_trunc('week', dates)::date, (date_trunc('week', dates) + interval '7 days')::date) week
FROM generate_series(date '2013-01-01', now(), '1 day')
dates ORDER BY 1
)
SELECT weeks.start,
count(whatever.*) as count
FROM whatever
JOIN weeks
ON weeks.week @> whatever.created_at::date
GROUP BY 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment