A slightly tricky SQL question (we are running SQL server 2000).
I have the following table, StoreCount -
WeekEndDate StoreCount
2010-07-25 359
2010-07-18 359
2010-07-11 358
2010-07-04 358
2010-06-27 358
2010-06-20 358
2010-06-13 358
2010-06-06 359
2010-05-30 360
2010-05-23 360
2010-05-16 360
I want to turn this into the following output -
StartDate EndDate StoreCount
2010-07-18 2010-07-25 359
2010-06-13 2010-07-11 358
2010-06-06 2010-06-06 359
2010-05-16 2010-05-30 360
As you can see, I'm wanting to group the store counts, by only as they run in sequence together.
try this simple solution:
sqlfiddle
Ok, here's my go at it.
Here's a kick at the can, only it may have syntax not available in SS2k. It was actually written on Oracle as I don't have that version of SS around anymore. The only catch might be the the select of a select...(it's been a while since I've used SS2k, so it's hard to remember what features weren't available back then.)
Use cursor. I don't know how to do it in sql2k by using query.
I'm not sure how to explain this, but it seems to give the desired result for the small dataset given. In essence, it detects the points in the series where the values change.
I haven't looked at the query plan, might be painful.
Tried on a Sybase server, so syntax should be compatible with SQL Server 2K.