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?

On Aug 30, 7:02 am, Bob S. [email protected]
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

On 8/30/07, Bob S. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs