我有一個查詢,可以識別sql server 2008 r2中日期範圍的差距和重疊。每個獨特的數據集有12個記錄。我想要做的是調整或添加識別差距和重疊的代碼,並將記錄更新爲順序。SQL間隙重疊DateRanges
--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
。 。出於某種原因,我可以用我想添加的代碼編輯您的問題,但我無法將其放入我的解決方案中。如果有人想把它搬下來,謝謝。無論如何,這是否做你想要的? –
我已經將Gordon寫到他的帖子中的代碼移走了。請檢查一下。 –