将多个期间分组为一个期间

时间:2018-10-24 15:14:59

标签: sql sql-server gaps-and-islands

我有一个时间表列表。每条记录都有一个ID,开始和结束日期/时间。

几条记录是背对背的。表示结束时间是下一条记录的开始时间。我需要将这些记录合并在一起以形成一个较长的时期。

我尝试了滞后/先导功能,尝试对初始ID进行分组,但没有成功。

这是数据的屏幕截图。 “ IsInAGroup”是我使用滞后/提前量的派生列。...

  

SELECT ID,PeriodStart,PeriodEnd,用例           LEAD(PeriodStart)结束(按PeriodStart按REG_NUMBER顺序进行分区)= PeriodEnd THEN              1个                   --ID滞后(PeriodEnd)超过(按PeriodStart按REG_NUMBER顺序进行分区)= PeriodStart THEN --ID 1       其他           空值       在#tmpACTIVITIES中以“ IsInAGroup”结尾,其中REG_NUMBER ='ABC123'并且PeriodStart> ='6/1/2018'按2排序

enter image description here

2 个答案:

答案 0 :(得分:1)

这会成功吗?也就是说,您只想将IsInAGroup设置为1,即存在另一个记录,该记录的开始或结束时间与问题的结束或开始时间相匹配:

update a
set IsInAGroup = 1
from myTable a
where exists 
(
    select top 1 1
    from myTable b
    where b.Id != a.Id --it's a different record
    and 
    (
        b.PeriodEnd = a.PeriodStart --but the record is immediately before our record
        or b.PeriodStart = a.PeriodEnd --or the record is immediately after
    )
)

更新

对于每个注释,如果您希望将一堆记录“压缩”为一个记录,请尝试使用递归CTE。

with cte as
(
    --get all periods which don't immediately follow another period
    --these are the first record in the group (including groups of 1 record)
    --NB: assumes that a single record cannot have its PeriodStart = its own PeriodEnd
    select Id, PeriodStart, PeriodEnd, 1 Iteration 
    from myTable
    where PeriodStart not in (select PeriodEnd from myTable)

    union all

    --recursively get each period with a start date matching the last record's end date.
    --persist the original id and start date, use the new record's end date, add 1 to the iteration column each recursion
    select cte.Id, cte.PeriodStart, mt.PeriodEnd, cte.Iteration + 1 
    from cte
    inner join myTable mt on mt.PeriodStart = cte.PeriodEnd
)
, cte2 as 
(
    --get all records / invert the Iteration (so the last record in a window has value r=1)
    select id, PeriodStart, PeriodEnd, row_number() over (partition by id order by Iteration desc) r
    from cte
)
--select all records where r=1 (i.e. the last created by the recursive cte, giving the largest range of start-to-end date for each id
select Id, PeriodStart, PeriodEnd
from cte2
where r = 1

希望这些评论可以解释发生了什么;但是如果您需要任何说明,请发表评论。

答案 1 :(得分:0)

with cte as
(
    --get all periods which don't immediately follow another period
    --these are the first record in the group (including groups of 1 record)
    --NB: assumes that a single record cannot have its PeriodStart = its own PeriodEnd
    select T1.ID, T1.START_TIME, T1.END_TIME, 1 Iteration,T1.REG_NUMBER 
    from 
        #tmpACTIVITIES T1
        LEFT JOIN #tmpACTIVITIES T2 ON (T1.REG_NUMBER=T2.REG_NUMBER) AND (T1.START_TIME=T2.END_TIME)
    WHERE 
        T2.ID IS NULL

    --where START_TIME not in (select END_TIME from #tmpACTIVITIES)

    union all

    --recursively get each period with a start date matching the last record's end date.
    --persist the original id and start date, use the new record's end date, add 1 to the iteration column each recursion
    select cte.ID, cte.START_TIME, mt.END_TIME, cte.Iteration + 1,cte.REG_NUMBER  
    from cte
    inner join #tmpACTIVITIES mt on (mt.REG_NUMBER=cte.REG_NUMBER) AND (mt.START_TIME = cte.END_TIME)
)
, cte2 as 
(
    --get all records / invert the Iteration (so the last record in a window has value r=1)
    select ID, START_TIME, END_TIME, REG_NUMBER ,row_number() over (partition by REG_NUMBER,ID order by Iteration desc) r
    from cte
)
--select all records where r=1 (i.e. the last created by the recursive cte, giving the largest range of start-to-end date for each id
select ID, START_TIME, END_TIME,REG_NUMBER 
from cte2
where r = 1