我有一个查询,用于识别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
答案 0 :(得分:1)
好吧,你可以根据下一个值为每条记录分配一个新的Read_end
。新开始的计算可以这样完成:
select t.*,
(select top 1 Read_Start
from t t2
where t2.e = t.e and t2.Read_Start > t.Read_Start
order by t2.Read_Start
) as New_Read_End
from t
你真的想更新价值还是只看它应该是什么?