Tuesday 15 May 2012

sql - Select count within date range on multiple tables -


I need a scope that can listen to me, download, and purchase within a date range for purchases Can I do it within a scope?

I have the scope of this railroad which allows me to talk at the top of the list of tracks within a certain range.

  scope: top_by_listens, lambda {| Count = zero, range_start = zero, range_end = zero | Adds (: listens). Group ('tracks.id'). Action ("Action_count" as track. *, Count (user_actions.id)). Where ('user_actions.created_at between? And?', Range_start || time now.bizening_off_moth, range_end || time.now.end_of_month) .order ("action_ count dess"). With this, I can call "action_count" with a track and count within the specified range.  

I want to be able to make some calls like "track.listens.action_count" and "track.downloads.action_count".

I know that I can join many couples like the joins (: listenens, downloads, orders) , but I'm 'action_count on three different organizations 'How can I get the calling ability?

Here's the beginning:

  Scope: top_by_all, lambda {| Count = zero, range_charts = zero, range_and = zero | Adds (: listens, downloads: order) .group ('track. Idid'). Selection ("track. *, Count (user_actions.id) ascation_count"). Where ('between user_actions.created_at? And?', Range_state || time.now.beginning_of_month, range_end || time.now.end_of_month) .order ("action_count DESC") .limit (count = 5)}  

Any help / appreciation of ideas!

I have not verified that your section and limit where the required work is but here is an example How can you solve your problem in a single query? I think it will be quite fast.

  scope: top_by_all, lambda {| Count = nil, range_start = zero, range_end = zero | Adds (: hears, downloads: order). Group ('Track I.ID') Selection ("tracks. *, Counting (listens separately.) + Count (separate downloadss.id) + count (specific order.) Action_count"). Where ('between user_actions.created_at? And?', Category_start || time.Nov.bizening_off_moth, range_and || time.now.end_of_month.) ("Action_ count dESC") .limit (count = 5)}  

No comments:

Post a Comment