Skip to content

Instantly share code, notes, and snippets.

@Coaden
Last active August 29, 2015 13:57
Show Gist options
  • Save Coaden/9639339 to your computer and use it in GitHub Desktop.
Save Coaden/9639339 to your computer and use it in GitHub Desktop.
the more complicated grouping/totaling by date
sql = "WITH dates_table AS ( " \
" SELECT created_at::date AS date_column FROM text_textdb WHERE stream_id=%s " \
") " \
"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, 1) AS offs, last_date from ( " \
"SELECT %s::date AS last_date, %s::date AS first_date " \
") 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