Skip to content

Instantly share code, notes, and snippets.

@xrl
Created March 18, 2013 23:32
Show Gist options
  • Save xrl/5191990 to your computer and use it in GitHub Desktop.
Save xrl/5191990 to your computer and use it in GitHub Desktop.
@report = ActiveRecord::Base.connection.execute(%Q{
SELECT events_with_objects.year_month
,events_with_objects.count
FROM (
SELECT generate_series(min(datetime)::date
,max(datetime)::date
,interval '1 month')::date AS tag
FROM events
) s
LEFT JOIN (SELECT year_month
,count(year_month)
FROM events
INNER JOIN event_objects
ON events.id = event_objects.event_id
AND event_objects.object_class = 'Provider'
AND event_objects.object_id = #{Cached::Event.connection.quote current_user.provider.id}
AND events.event = 'view'
GROUP BY 1
) events_with_objects
ON events_with_objects.year_month = to_char(s.tag,'YYYY/MM')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment