[英]Finding gap between dates in sql for a particular month
I have 4 columns `BenefitKey, MemberKey, StartDate, Enddate` in a table Given data like this: StartDate Enddate ------------------------------ 20110315 20110316 20110317 20110320 20110321 20110325 20110326 20121202 20121203 20121210 20121211 20121215 20121225 20121231 I need to find the missing gap between the month of December and fill the gap using a SQL query Here the missing gap is from `20121216` to `20121224`. I have 1000 rows like this, so I need a SQL query .i found some solution to it but still not correct here is what i wrote CREATE TABLE #BenfitDim(MemberName varchar(30),Memberkey int,MemberEffectiveDate DATETIME, MemberTerminationDate DATETIME) INSERT INTO #BenfitDim VALUES('tom',231,'2012-11-18','2012-11-23') INSERT INTO #BenfitDim VALUES('tom',231,'2012-11-24','2012-12-12') INSERT INTO #BenfitDim VALUES('tom',231,'2013-01-01','2999-12-12') INSERT INTO #BenfitDim VALUES('jack',344,'2011-06-27','2012-12-07') INSERT INTO #BenfitDim VALUES('jack',344,'2012-12-01','2015-12-31') INSERT INTO #BenfitDim VALUES('nick',243,'2012-12-01','2012-12-07') INSERT INTO #BenfitDim VALUES('joy',234,'2012-12-08','2012-12-14') INSERT INTO #BenfitDim VALUES('tim',364,'2012-12-25','2012-12-30') INSERT INTO #BenfitDim VALUES('tim',364,'2013-01-15','2013-01-30') INSERT INTO #BenfitDim VALUES('jerry',365,'2011-9-15','2012-12-31') INSERT INTO #BenfitDim VALUES('jerry',365,'2013-01-15','2013-01-30') INSERT INTO #BenfitDim VALUES('jerry',365,'2011-01-15','2012-01-30') SELECT MemberKey, MemberName, DATEADD(DAY,1,T1.MemberTerminationDate)AS MemberEffectiveDate, DATEADD(DAY,-1,D.MemberEffectiveDate)AS MemberTerminationDate FROM #BenfitDim AS T1 CROSS APPLY( SELECT MIN(MemberEffectiveDate)AS MemberEffectiveDate FROM #BenfitDim AS T WHERE T.MemberEffectiveDate > T1.MemberEffectiveDate AND T.MemberKey = T1.MemberKey)D WHERE DATEADD(DAY,1,T1.MemberTerminationDate) D.MemberEffectiveDate Once you execute you will find the missing sequence but still there is slight problem how do we take care of an overlap data of "jack" in the table and get the missing sequence right .
您是否有一個包含完整年份的表? 如果沒有,則需要創建一個以查找丟失的日期。 否則,您可以使用非常慢的游標。
假設您的表格名為“好處”。
沿着這條路線可以解決您的問題嗎?
SELECT A.EndDate + 1 AS GapFrom,
(SELECT MIN(StartDate) - 1
FROM Benefits WHERE StartDate > A.EndDate) AS GapUntil
FROM Benefits A
LEFT JOIN Benefits B WHERE A.EndDate + 1 = B.StartDate
WHERE B.BenefitKey IS NULL
您想要做的是一個自我聯接,您要在其中聯接先前的記錄,例如:
SELECT …,startDate, endDate
FROM Table t, Table t2
WHERE t2.id=t1.id+1
(這假設ID是順序的,如果不是,則執行以下操作:
WHERE t1.enddate < MIN(t1.startdate)
這應該為您提供按日期順序排列的下一條記錄。
然后,要塞孔就很簡單:
INSERT INTO table
VALUES blah, blah, t.endDate+1, t2.startDate-1
(日期上的偽代碼,您必須使用您的版本支持的任何功能(例如DATEADD)進行數學運算。)
如果您的數據采用這種格式,並且沒有重疊,那么以下查詢將為您提供兩個日期之間的間隔:
select enddate+1 as startdate,
(select MIN(startdate) - 1 from t t2 where t2.startdate > t.enddate
) as enddate
from t
where enddate+1 not in (select startdate from t)
我在這里使用相關的子查詢,因為我認為它使查詢的結構更清晰。 間隔從enddate+1
開始,到下一個開始日期的一天結束。 而且,當enddate+1
本身不是開始日期時,只有一個間隙。
如果產生正確的結果,則可以使用以下命令將這些值插入表中:
insert into t(startdate, enddate)
select enddate+1 as startdate,
(select MIN(startdate) - 1 from t t2 where t2.startdate > t.enddate
) as enddate
from t
where enddate+1 not in (select startdate from t)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.