I have an event record database, I need to find all events for each costumer and group them in one week interval from the last record of each costumer until the first.
FirstEvent LastEvent Client
==
2011-01-01 06:55:21.000 |2011-04-30 21:46:09.000 |Client1
2011-01-01 06:20:00.000 |2011-02-23 12:43:55.000 |Client2
2011-01-03 08:34:33.000 |2011-04-30 09:00:39.000 |Client3
2011-01-01 02:14:45.000 |2011-04-30 15:31:41.000 |Client4
2011-01-01 08:08:12.000 |2011-02-21 09:41:28.000 |Client5
2011-02-01 11:29:28.000 |2011-04-29 09:13:25.000 |Client6
Now I have to count the records of each client and group them like this
Client From To Records
==
Client1 |2011-04-30 21:46:09.000 |2011-04-23 21:46:09.000 |200
Client1 |2011-04-16 21:46:09.000 |2011-04-09 21:46:09.000 |400
...Until the the first date
Client1 |2011-01-08 06:55:21.000 |2011-01-01 06:55:21.000 |250
Client2 |2011-02-23 12:43:55.000 |2011-02-16 12:43:55.000 |50
...The same for each user
I hope someone can help me find a way to output this.
Okay, I think this does exactly what you wish for:
Though, if there are no records a certain week, it will leave out that row instead of returning a row containing the Records value of 0:
Edit:
If you wonder why I use
DAYinstead ofWEEKfor myDATEDIFF, it is to avoid problems whether Sunday or Monday is the first day of the week.