Bob Sanders
2007-Aug-30 14:02 UTC
How do you calculate the max # of sales in a 5 day period?
If I''m trying to calculate the record (max) number of sales within a 5 day period, how would I do it? I''m guessing it starts with a Sales.maximum, but being an absolute noob, I have no idea what next. Maybe like a Sales.maximum(:group => 5.days) --- or something similar? I''m probably completely wrong with my approach. Any ideas? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gene.tani-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Aug-30 14:17 UTC
Re: How do you calculate the max # of sales in a 5 day period?
On Aug 30, 7:02 am, Bob Sanders <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> If I''m trying to calculate the record (max) number of sales within a 5 > day period, how would I do it? I''m guessing it starts with a > Sales.maximum, but being an absolute noob, I have no idea what next. > > Maybe like a Sales.maximum(:group => 5.days) --- or something similar? > I''m probably completely wrong with my approach. > > Any ideas? > -- > Posted viahttp://www.ruby-forum.com/.if you have an array of daily sales, you can each_cons over it: http://stdlib.rubyonrails.org/libdoc/enumerator/rdoc/index.html --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Showalter
2007-Aug-30 15:58 UTC
Re: How do you calculate the max # of sales in a 5 day period?
On 8/30/07, Bob Sanders <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > If I''m trying to calculate the record (max) number of sales within a 5 > day period, how would I do it? I''m guessing it starts with a > Sales.maximum, but being an absolute noob, I have no idea what next.What do you mean when you say "max?" Which day of 5 specific days had the most sales? Does your table have one row per day or one row per sale? Assuming you had one row per sale, then the number of sales per day would be given by: select date, count(*) from sales group by date To restrict to a 5-day window, you use a where clause: select date, count(*) from sales where date between ? and ? group by date To get the date with the highest, you can do something like this with MySQL: select date, count(*) from sales where date between ? and ? group by date order by 2 desc limit 1 In AR you write that as: Sales.count :conditions => [''date between ? and ?'', d1, d2], :group => ''date'', :order => ''2 desc'', :limit => 1 That will return an array with one element, which in turn is an array with two elements: a date, and a number representing the count of rows for that date. HTH --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---