我有一个查询可以识别 sql server 2008 r2 中日期范围的间隙和重叠。每个唯一数据集有 12 条记录。我想做的是调整或添加识别间隙和重叠的代码,并将记录更新为连续的。
--gaps and overlaps tbl_volumes
with s as
(
select esiid,Read_Start,Read_End ,row_number() over(partition by esiid order by Read_Start) rn
from tbl_Volumes
where Status=0
group by esiid,Read_Start,Read_End)
select a.esiid, a.Read_Start, a.Read_End, b.Read_Start as nextstartdate,datediff(d,a.Read_End, b.Read_Start) as gap
into #go
from s a
join s b on b.esiid = a.esiid and b.rn = a.rn + 1
where datediff(d, a.Read_End, b.Read_Start) not in (0,1)
order by a.esiid
这是我希望看到的坏记录集:
e Read_Start Read_End Source
10032789402145965 2011-01-21 2011-02-22 867_03_1563303
10032789402145965 2011-02-22 2011-03-21 867_03_1665865
10032789402145965 2011-03-26 2011-04-20 867_03_1782993