Please see the sample table with some data below:
Declare @tmp table (ID int identity(1,1) not null,Name Varchar(5), StartDate Datetime, EndDate Datetime) Insert into @tmp Select 'A', '2012-11-01','2012-11-15' Union Select'A', '2012-11-18','2012-11-30' Union Select'A', '2012-12-05','2012-12-07' Union Select'B', '2012-11-21','2012-11-25' Union Select'B', '2012-11-27','2012-11-30' Union Select'C', '2012-11-27','2012-11-30' Select * from @tmp
Based on that data i want some new information as shown in table below how can i achieve this
ID | Name | StartDate | EndDate | NewStartDate | NewEndDate |
1 | A | 2012-11-01 00:00:00.000 | 2012-11-15 00:00:00.000 | 2012-11-01 00:00:00.000 | 2012-11-17 00:00:00.000 |
2 | A | 2012-11-18 00:00:00.000 | 2012-11-30 00:00:00.000 | 2012-11-18 00:00:00.000 | 2012-12-04 00:00:00.000 |
3 | A | 2012-12-05 00:00:00.000 | 2012-12-07 00:00:00.000 | 2012-12-05 00:00:00.000 | 2012-12-9 00:00:00.000 |
4 | B | 2012-11-21 00:00:00.000 | 2012-11-25 00:00:00.000 | 2012-11-21 00:00:00.000 | 2012-11-26 00:00:00.000 |
5 | B | 2012-11-27 00:00:00.000 | 2012-11-30 00:00:00.000 | 2012-11-27 00:00:00.000 | 2012-12-9 00:00:00.000 |
6 | C | 2012-11-27 00:00:00.000 | 2012-11-30 00:00:00.000 | 2012-11-27 00:00:00.000 | 2012-12-9 00:00:00.000 |