algorithm advice for finding maximum items within a time period

Posted by darren on Stack Overflow See other posts from Stack Overflow or by darren
Published on 2010-03-18T19:39:35Z Indexed on 2010/03/18 19:41 UTC
Read the original article Hit count: 256

Filed under:
|

Hi everyone. I have a database schema that is similar to the following:

| User   | Event         | Date
|--------|---------------|------
| 111    | Walked dog    | 2009-10-1
| 222    | Walked dog    | 2009-10-2
| 333    | Fed Fish      | 2009-10-5
| 222    | Did Laundry   | 2009-10-6
| 111    | Fed Fish      | 2009-10-7
| 111    | Walked dog    | 2009-10-18
| 222    | Walked dog    | 2009-10-19
| 111    | Fed Fish      | 2009-10-21

I would like to produce a query that returns the maximum number of times a user performs some action within a time period. For example, given a time period of 5 days, what is the maximum number of times user 111 walked the dog?

The most obvious solution would be to start at some zero point and move forward each day, summing up 5 day periods along the way, then taking the maximum total out of all the 5 day windows. the approach seems incredibly costly however.

I would appreciate any suggestions you may have.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about database