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.
EDIT 1:
Thanks for the comments / answers. To respond:
- I'm using mySQL v5.0
- There could be any number of events per day (per any time period really)
- @Paulo Santos: thanks,but like the comment points out, i need to find the window that produces the most results, the window itself can slide.
- @Mark: this looks like an interesting solution, although i recall reading that mySQL does not support backing up or jumping ahead cursors.
- @orbMan: this looks promising. I don't fully understand it yet, but I will give this a try tonight.
- @mjv: another promising solution. also looks complicated, but I will give it another look
thanks again!