我需要找到 12 月份之间的缺失差距并使用 SQL 查询来填补空白
我有以下查询,它需要 tweeking 因为它仍然不正确,这是我写的
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
执行后,您会发现缺少的序列,但仍然存在小问题。我们如何处理表中“jack”的重叠数据并正确获取缺失的序列。感谢您的帮助。