Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 server TSQL-组和岛日期_Sql Server_Tsql_Datetime_Gaps And Islands - Fatal编程技术网

Sql server TSQL-组和岛日期

Sql server TSQL-组和岛日期,sql-server,tsql,datetime,gaps-and-islands,Sql Server,Tsql,Datetime,Gaps And Islands,我需要一个关于为下面的问题编写最佳查询的帮助。我已经附上了我随身携带的查询,但它高度利用了资源 下面是实现上述逻辑的代码。请提出一些实现同样目标的最佳方法 -- drop table #me create table #ME (memid int , EffectiveDate datetime , termdate datetime) Insert into #ME values ('123','3-Dec-16','10-Jan-17') Insert into #ME values ('

我需要一个关于为下面的问题编写最佳查询的帮助。我已经附上了我随身携带的查询,但它高度利用了资源

下面是实现上述逻辑的代码。请提出一些实现同样目标的最佳方法

-- drop table #me
create table #ME (memid int , EffectiveDate datetime , termdate datetime)

Insert into #ME values ('123','3-Dec-16','10-Jan-17')
Insert into #ME values ('123','11-Jan-17','6-Feb-17')
Insert into #ME values ('123','7-Feb-17','5-Mar-17')
Insert into #ME values ('123','8-Mar-17','15-Apr-17')
Insert into #ME values ('123','16-Apr-17','24-May-17')

--drop table #dim
select * from #ME
declare @StartDate datetime , @CutoffDate datetime

select @StartDate= min(effectivedate),@CutoffDate = max(termdate) From #me where termdate<>'9999-12-31 00:00:00.000'

SELECT d
 into #dim
FROM
(
  SELECT d = DATEADD(DAY, rn - 1, @StartDate)
  FROM 
  (
    SELECT TOP (DATEDIFF(DAY, @StartDate, @CutoffDate)) 
      rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id])
    FROM sys.all_objects AS s1
    CROSS JOIN sys.all_objects AS s2
    -- on my system this would support > 5 million days
    ORDER BY s1.[object_id]
  ) AS x
) AS y;

--drop table #MemEligibilityDateSpread

select MemID, D As DateSpread Into #MemEligibilityDateSpread From #Dim dim JOIN #me ME on dim.d  between ME.effectivedate and me.termdate 

--drop table #DateClasified

WITH CTE AS 
(
 SELECT MEmID,
        UniqueDate = DateSpread, 
        DateGroup  = DATEADD(dd, - ROW_NUMBER() OVER (PARTITION BY Memid ORDER BY Memid,DateSpread), DateSpread)
  FROM #MemEligibilityDateSpread
  GROUP BY Memid,DateSpread
)
--===== Now, if we find the MIN and MAX date for each DateGroup, we'll have the
     -- Start and End dates of each group of contiguous daes.  While we're at it,
     -- we can also figure out how many days are in each range of days.
 SELECT Memid,
        StartDate = MIN(UniqueDate),
        EndDate   = MAX(UniqueDate) 
   INTO #DateClasified
   FROM cte 
  GROUP BY Memid,DateGroup
  ORDER BY Memid,StartDate

select ME.MemID,ME.EffectiveDate,ME.TermDate,DC.StartDate,DC.EndDate from #DateClasified dc join #me ME ON  Me.MemID = dc.MemID 
        and (ME.EffectiveDate BETWEEN DC.StartDate AND DC.EndDate
                OR ME.TermDate BETWEEN DC.StartDate AND DC.EndDate) 

在cte0和cte1中,我们创建一个临时理货/日历表。一旦我们有了这些,就可以按岛屿进行计算和分组了

目前,理货表的最大期限为10000天27年,但您可以通过添加cte0 N5轻松扩展理货表

返回

MemID   EffectiveDate   TermDate    SinceFrom   Tildate 
123     2016-12-03      2017-01-10  2016-12-03  2017-03-05  
123     2017-01-11      2017-02-06  2016-12-03  2017-03-05  
123     2017-02-07      2017-03-05  2016-12-03  2017-03-05  
123     2017-03-08      2017-04-15  2017-03-08  2017-05-24  
123     2017-04-16      2017-05-24  2017-03-08  2017-05-24  
MemID   EffectiveDate   TermDate
123     2016-12-03      2017-03-05
123     2017-03-08      2017-05-24
编辑-如果需要压缩数据集,请立即编辑

返回

MemID   EffectiveDate   TermDate    SinceFrom   Tildate 
123     2016-12-03      2017-01-10  2016-12-03  2017-03-05  
123     2017-01-11      2017-02-06  2016-12-03  2017-03-05  
123     2017-02-07      2017-03-05  2016-12-03  2017-03-05  
123     2017-03-08      2017-04-15  2017-03-08  2017-05-24  
123     2017-04-16      2017-05-24  2017-03-08  2017-05-24  
MemID   EffectiveDate   TermDate
123     2016-12-03      2017-03-05
123     2017-03-08      2017-05-24

我已经在我的本地机器上运行过了,运行在远程服务器上时,它会产生大约130条记录。没有任何变化,但在不同的机器上,它会产生不同的输出。有什么想法吗?相同的输入,相同的查询,但不同的输出使CTE1作为temp&ran,现在工作正常。知道为什么它不能作为cte工作吗?@vignesh不知道为什么cte会给你带来问题。对我来说毫无意义。@vignesh只是一个想法。。。您是否在使用之前删除了分号?如果是这样,就把它放回去