| Sequential Series Update Query I'm using PHP 5.28 with SQL Server 2005
I'm stuck on a query. I have a task management system with time ranges and I need to recalculate and update all affected records such that every start and stop time is consecutive. Currently, if they user wants to enter a time that conflicts with another, he has to insert a new record with a new time range.
The problem: the user enters a time that falls between an existing time range or one that encompass multiple time ranges.
Existing Table Entries: START | STOP
1) 8:00 - 9:00
2) 9:00 - 10:30
3) 10:30 - 12:00
4) 12:00 - 3:00
Let's say he wants to update his task table to say that between 9:30 and 10:00 he was on a coffee break, or another case is where he wants to enter a time between 8:30 and 10:00. What I want is for the database to know that the new start time (9:30) replaces the stop time of record (2) and the start time of record (3) and so on.
I want it to look like this: START | STOP
1) 8:00 - 9:30
2) 9:30 - 10:00
3)10:00 - 10:30
4) 10:30 - 12:00
5) 12:00 - 3:00
I don't know if I should figure out the order in PHP or if there's some tricky SQL I can do, or how many rows really need to be altered. Either way, I feel like I'm reinventing the wheel on this one, someone must have done this before.
Clear as mud? |