試試下面的SQL:
這是概念:
- 修復的id值,此刻這是一團糟。
- 創建一個映射表來幫助我們獲得結果。
注:請重新檢查SQL(表名,字段名等)
-- Declare TempData Table
declare @tempData table
(
id int identity,
[sid] int,
start date,
[end] date
);
-- Declare TempMapping Table
declare @tempMapping table
(
id int identity,
start_id int,
end_id int
);
--Insert the data to @tempData, with new ID
insert into @tempData([sid],[start],[end])
select [sid],[start],[end] from @temp
order by [sid],[start],[end]
;
--insert The Mapping value
insert into @tempMapping(start_id,end_id)
select
start_id , end_id
from
(
select start_id, end_id
, ROW_NUMBER() over (partition by end_id order by start_id) as xxrow
from
(
select
a.id as start_id ,bb.id as end_id
,ROW_NUMBER() over(partition by a.id order by a.id) as xrow
from
(
select b.id, b.[sid],b.[start]
from @tempData b
where b.[end] is null
) a
left join
(
select b.id, b.[sid],b.[start],b.[end]
from @tempData b
where b.[end] is not null
) bb on a.[sid] = bb.[sid] and a.[id]< bb.id
group by
a.id, a.[sid],a.[start]
,bb.[id]
) c
where c.xrow = 1
) d
where d.xxrow =1
order by start_id, end_id
;
/*
select * from @tempMapping
--Table Mapping Result:
id start_id end_id
----------- ----------- -----------
1 2 4
2 5 8
3 11 12
4 13 NULL
*/
--Result Query
select
e.[id],e.[sid], e.[start],e.[end]
from @tempData e
left join @tempMapping f on f.end_id = e.id
where e.[end] is not null and f.id is null
union
select
start_id as id, b.sid, b.start, c.[end]
from @tempMapping a
left join @tempData b on a.start_id = b.id
left join @tempData c on a.end_id = c.id
結果表:
id sid start end
----------- ----------- ---------- ----------
1 10017 2006-01-11 2006-06-28
2 10017 2006-10-24 2007-12-31
5 10017 2008-04-15 2010-03-01
9 10019 2005-09-01 2005-10-03
10 10019 2006-03-15 2006-09-15
11 10019 2006-10-01 2007-04-12
13 10019 2010-01-02 NULL