Skip to content

Instantly share code, notes, and snippets.

@Coaden
Last active August 29, 2015 13:57
Show Gist options
  • Save Coaden/9635125 to your computer and use it in GitHub Desktop.
Save Coaden/9635125 to your computer and use it in GitHub Desktop.
sql to group by date :: raw example
WITH dates_table AS (
SELECT created::date AS date_column FROM sharer_emailshare WHERE showroom_id=5
)
SELECT series_table.date, COUNT(dates_table.date_column), SUM(COUNT(dates_table.date_column)) OVER (ORDER BY series_table.date) FROM (
SELECT (last_date - b.offs) AS date
FROM (
SELECT GENERATE_SERIES(0, last_date - first_date, 1) AS offs, last_date from (
SELECT MAX(date_column) AS last_date, (MAX(date_column) - '1 year'::interval)::date AS first_date FROM dates_table
) AS a
) AS b
) AS series_table
LEFT OUTER JOIN dates_table
ON (series_table.date = dates_table.date_column)
GROUP BY series_table.date
ORDER BY series_table.date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment