Last active
August 29, 2015 13:57
-
-
Save Coaden/9635125 to your computer and use it in GitHub Desktop.
sql to group by date :: raw example
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
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
http://stackoverflow.com/questions/15691127/postgresql-query-to-count-group-by-day-and-display-days-with-no-data