Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql中查找特定月份的日期间隔_Sql_Sql Server_Tsql - Fatal编程技术网

在sql中查找特定月份的日期间隔

在sql中查找特定月份的日期间隔,sql,sql-server,tsql,Sql,Sql Server,Tsql,你们有一张有完整年份日期的桌子吗?如果没有,您需要创建一个来查找丢失的日期。否则,您可以使用一个非常慢的光标。假设您的表名为Benefits 这样做能解决你的问题吗 I have 4 columns `BenefitKey, MemberKey, StartDate, Enddate` in a table Given data like this: StartDate Enddate ------------------------------ 20

你们有一张有完整年份日期的桌子吗?如果没有,您需要创建一个来查找丢失的日期。否则,您可以使用一个非常慢的光标。

假设您的表名为Benefits

这样做能解决你的问题吗

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
这假设ID是连续的,如果不是,则执行以下操作:

SELECT …,startDate, endDate
  FROM Table t, Table t2
 WHERE t2.id=t1.id+1

对日期进行伪代码,您必须使用您的版本支持的任何函数(如DATEADD)进行计算。

如果您的数据具有这种格式,并且没有重叠,那么以下查询将为您提供两个日期之间的间隔:

INSERT INTO table
   VALUES blah, blah, t.endDate+1, t2.startDate-1
我在这里使用相关子查询,因为在我看来,它使查询的结构更清晰。间隔从enddate+1开始,在下一个开始日期前一天结束。而且,只有enddate+1本身不是startdate时才有间隙

如果生成的结果正确,则可以使用以下方法将这些值插入到表中:

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)

如何编写sql查询来查找差距?我没有完整的日期年份表。我有一百万条记录要拉,所以光标不会被移动accepted@user2007879然后你需要创建一个完整的日期年份。sql server的哪个版本?我使用的是sql server 2008。你还在考虑这里的答案吗?CTE有一种快速简便的方法。如果您感兴趣,我很乐意将其记录下来作为答案。
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)
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)