[long post]
Ha I was just about to post the same thing. I think you could accomplish it in pure SQL, but using VB is probably a better option. This is how I would approach your problem:
To start I would create an empty table with the exact same schema as yours, except add an incrementing identity field and a field called [CovIndicator] or something. Then insert into your new table something like this:
Code:
select [ID], start, end
from table
order by [ID], start, end
This will get everything in a proper order for you, so that you can loop through records while incrementing a counter. This counter will always be equal to your Identity field in the new table, so you will always be looking at the next record.
So your program will start with record 1, and assign CovIndicator = 1. Then it gets tricky.
You'll want to look at the ID for the next record. If it is different ID, then you again assign CovIndicator =1.
If ID is the same, then you look at the previous row's end date. If [current record's start date] = dateadd(d, 1, [previous record end]) then you again assign CovIndicator = 1.
If [current record's start date] <> dateadd(d, 1, [previous record end]) then you will want to increment CovIndicator to equal [Previous CovIndicator] + 1, in this case 2.
As you can see, this would take some time to write and validate, and would contain several variables (and with them a lot of incrementing to keep track of). It would also run incredibly slowly.
When it is through running, you would have the additional field you want to group on for your query though.
Code:
select [ID], CovIndicator, min(start) as StartDate, max(end) as EndDate
from TABLE
group by [ID], CovIndicator
Then you just need to tweak your front end so that when a record is added it looks in your table to see what CovIndicator should be assigned.
I will also be very interested to see your eventual solution, please keep us posted.
Alex
[/long post]
Professor: But what about your superintelligence?
Gunther: When I had that there was too much pressure to use it. All I want out of life is to be a monkey of moderate intelligence who wears a suit. That's why I've decided to transfer to Business School.
Professor: NOOOOOOOOOOOO.