Skip to content

Instantly share code, notes, and snippets.

@Coaden
Created March 19, 2014 12:21
Show Gist options
  • Save Coaden/9640538 to your computer and use it in GitHub Desktop.
Save Coaden/9640538 to your computer and use it in GitHub Desktop.
Simpler gouping by date,and easier for changing to hours and later adding the join to the classification DB
sql = "SELECT t.date, COUNT(se.id) FROM (" \
" SELECT to_char(date_trunc('day', (%s::TIMESTAMP - offs )), 'YYYY-MM-DD')" \
" AS date" \
" FROM GENERATE_SERIES(0, %s, 1)" \
" AS offs" \
") t " \
"LEFT OUTER JOIN (" \
" SELECT id, created_at::TIMESTAMP FROM text_textdb" \
" WHERE stream_id=%s" \
") se " \
"ON (t.date=to_char(date_trunc('day', se.created_at), 'YYYY-MM-DD')) " \
"GROUP BY t.date " \
"ORDER BY t.date"
sql = "SELECT t.date, COUNT(se.id) FROM (" \
" SELECT to_char(date_trunc('hour', (%s::TIMESTAMP - '1 HOUR'::INTERVAL * offs )), 'YYYY-MM-DD HH24')" \
" AS date" \
" FROM GENERATE_SERIES(0, %s, 1)" \
" AS offs" \
") t " \
"LEFT OUTER JOIN (" \
" SELECT id, created_at::TIMESTAMP FROM text_textdb" \
" WHERE stream_id=%s" \
") se " \
"ON (t.date=to_char(date_trunc('hour', se.created_at::TIMESTAMP), 'YYYY-MM-DD HH24')) " \
"GROUP BY t.date " \
"ORDER BY t.date"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment