# How do you calculate the max # of sales in a 5 day period?

**URL:** <https://www.ruby-forum.com/t/how-do-you-calculate-the-max-of-sales-in-a-5-day-period/113386>\
**Category:** Rails\
**Created:** [August 30, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/how-do-you-calculate-the-max-of-sales-in-a-5-day-period/113386 "2007-08-30T16:02:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 30, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/how-do-you-calculate-the-max-of-sales-in-a-5-day-period/113386/1 "2007-08-30T16:02:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 30, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/how-do-you-calculate-the-max-of-sales-in-a-5-day-period/113386/2 "2007-08-30T16:17:37Z")

</div>

On Aug 30, 7:02 am, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://stdlib.rubyonrails.org/libdoc/enumerator/rdoc/index.html)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 30, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/how-do-you-calculate-the-max-of-sales-in-a-5-day-period/113386/3 "2007-08-30T17:59:50Z")

</div>

On 8/30/07, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
