==============
declare @tbl table
(
category int,
startdate datetime,
enddate datetime
)
insert into @tbl
select 10, '1/1/2010 17:49:59', '10/30/2010 17:49:49' union all
select 10, '5/3/2010 17:49:59', '10/30/2010 17:49:59' union all
select 11, '1/1/2010 17:49:59', '10/30/2011 17:49:59' union all
select 11, '5/3/2010 17:49:59', '10/30/2010 17:49:59'
; with cte as
(
select category, dte = startdate, f = 'S'
from @tbl
union
select category, dte = enddate, f = 'E'
from @tbl
),
cte2 as
(
select *, row_no = row_number() over (partition by category order by dte)
from cte
)
select category = c1.category,
startdate = case when c1.f = 'E'
then dateadd(S, 1, c1.dte)
else c1.dte
end,
enddate = case when c2.f = 'S'
then dateadd(s, -1, c2.dte)
else c2.dte
end
from cte2 c1
inner join cte2 c2 on c1.category = c2.category
and c1.row_no = c2.row_no - 1
category startdate enddate ----------- ----------------------- ----------------------- 10 2010-01-01 17:49:59.000 2010-05-03 17:49:58.000 10 2010-05-03 17:49:59.000 2010-10-30 17:49:49.000 10 2010-10-30 17:49:50.000 2010-10-30 17:49:59.000 11 2010-01-01 17:49:59.000 2010-05-03 17:49:58.000 11 2010-05-03 17:49:59.000 2010-10-30 17:49:59.000 11 2010-10-30 17:50:00.000 2011-10-30 17:49:59.000 (6 row(s) affected) But my results should show like the following. category startdate enddate - - - 10 2010-01-01 17:49:59.000 2010-05-03 17:49:58.000 10 2010-05-03 17:49:59.000 2010-10-30 17:49:59.000 11 2010-01-01 17:49:59.000 2010-05-03 17:49:58.000 11 2010-05-03 17:49:59.000 2010-10-30 17:49:59.000 11 2010-10-30 17:50:00.000 2011-10-30 17:49:59.000 (5 row(s) affected) Because for the 10 seconds, no need to get the new one. I should be combined with the previous one. The date range actually ends at 17:49:59 only. Any suggestion on this?... Your help is greatly appreciated. --- [Edit] Thanks Squirrel for your help. I am giving some more cases here. For this case select 10, '1/1/2010 17:49:59', '10/30/2010 17:49:59' union all select 10, '5/3/2010 17:49:59', '10/30/2010 17:49:29' union all select 11, '1/1/2010 17:49:59', '10/30/2011 17:49:59' union all select 11, '5/3/2010 17:49:59', '10/30/2010 17:49:59' The results should be like the following and it works fine. category startdate enddate 10 2010-01-01 17:49:59.000 2010-05-03 17:49:58.000 10 2010-05-03 17:49:59.000 2010-10-30 17:49:29.000 10 2010-10-30 17:49:30.000 2010-10-30 17:49:59.000 11 2010-01-01 17:49:59.000 2010-05-03 17:49:58.000 11 2010-05-03 17:49:59.000 2010-10-30 17:49:59.000 11 2010-10-30 17:50:00.000 2011-10-30 17:49:59.000 (6 row(s) affected) But for this case select 10, '1/1/2010 17:49:59', '10/30/2010 17:49:29' union all select 10, '5/3/2010 17:49:59', '10/30/2010 17:49:59' union all select 11, '1/1/2010 17:49:59', '10/30/2011 17:49:59' union all select 11, '5/3/2010 17:49:59', '10/30/2010 17:49:59' union all select 12, '1/1/2010 17:49:59', '10/30/2010 17:49:09' union all select 12, '5/3/2010 17:49:59', '10/30/2010 17:49:59' I should get the results like the following. I am not getting this with the SQL which I have. category startdate enddate 10 2010-01-01 17:49:59.000 2010-05-03 17:49:58.000 10 2010-05-03 17:49:59.000 2010-10-30 17:49:59.000 11 2010-01-01 17:49:59.000 2010-05-03 17:49:58.000 11 2010-05-03 17:49:59.000 2010-10-30 17:49:59.000 11 2010-10-30 17:50:00.000 2011-10-30 17:49:59.000 12 2010-01-01 17:49:59.000 2010-05-03 17:49:58.000 12 2010-05-03 17:49:59.000 2010-10-30 17:49:59.000 (7 row(s) affected)