문제

I have a table of items which run sequentially by date with both a start and end for each entry:

ID | Start            | End
----------------------------------------
1  | 2012-08-20 00:00 | 2012-08-20 23:59 
2  | 2012-08-21 00:00 | 2012-08-21 23:59 
3  | 2012-08-22 00:00 | 2012-08-22 23:59 
4  | 2012-08-23 00:00 | 2012-08-23 23:59 
5  | 2012-08-24 00:00 | 2012-08-24 23:59 
6  | 2012-08-28 00:00 | 2012-08-28 23:59 
7  | 2012-08-29 00:00 | 2012-08-29 23:59 
8  | 2012-08-30 00:00 | 2012-08-30 23:59

Note that there are gaps in the data (for a weekend and a bank holiday). Is it possible to write a query which selects the records at the edges of the gaps? What I want is a general purpose way to end up with something like this:

ID | Start            | End
----------------------------------------
1  | 2012-08-20 09:00 | 2012-08-20 23:59 
2  | 2012-08-21 00:00 | 2012-08-21 23:59 
3  | 2012-08-22 00:00 | 2012-08-22 23:59 
4  | 2012-08-23 00:00 | 2012-08-23 23:59 
5  | 2012-08-24 00:00 | 2012-08-24 17:00 
6  | 2012-08-28 09:00 | 2012-08-28 23:59 
7  | 2012-08-29 00:00 | 2012-08-29 23:59 
8  | 2012-08-30 00:00 | 2012-08-30 17:00

Note that the start time of 1 and 6 has been updated, as has the end time of 5 and 8.

In the real database I am forced to used this day by day data layout as it has to retain compatibility with a legacy client, so I can't simply insert the ranges directly. Also in the real data I could be dealing with several hundred of these records at a time with multiple gaps.

The target platform is SQL2005/2008, though bonus points for solutions which work in SQL2000 compatibility mode.

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top