私はあなたが始めるための大まかなスクリプトを作成しました。日時を微調整する必要はなく、エンドポイントの比較には微調整が必要になる場合があります。
select P_ID, src.starttime, endtime = case when src.starttime <> lst.starttime or lst.starttime < DATEADD(dd,-1 * @gap,GETDATE()) then lst.starttime else GETDATE() end, frst.starttime, lst.starttime from @SOURCETABLE src outer apply (select starttime = MIN(starttime) from @SOURCETABLE sub where src.p_id = sub.p_id and sub.starttime > DATEADD(dd,-1 * @gap,src.starttime)) frst outer apply (select starttime = MAX(starttime) from @SOURCETABLE sub where src.p_id = sub.p_id and src.starttime > DATEADD(dd,-1 * @gap,sub.starttime)) lst where src.starttime = frst.starttime order by P_ID, src.starttime
プレ>次の出力が得られます。これはあなたのものとは少し異なりますが、問題ないと思います:
P_ID starttime endtime starttime starttime ----------- ----------------------- ----------------------- ----------------------- ----------------------- 12121 2009-03-24 07:30:00.000 2009-03-24 14:25:00.000 2009-03-24 07:30:00.000 2009-03-24 14:25:00.000 12345 2011-06-27 10:00:00.000 2011-07-21 09:00:00.000 2011-06-27 10:00:00.000 2011-07-21 09:00:00.000 12345 2011-09-21 12:00:00.000 2011-09-21 17:00:00.000 2011-09-21 12:00:00.000 2011-09-21 17:00:00.000 12345 2012-07-07 14:00:00.000 2012-07-07 14:00:00.000 2012-07-07 14:00:00.000 2012-07-07 14:00:00.000 12345 2012-08-13 13:00:00.000 2012-08-16 11:23:25.787 2012-08-13 13:00:00.000 2012-08-13 13:00:00.000 45454 2010-07-12 08:00:00.000 2010-07-12 08:00:00.000 2010-07-12 08:00:00.000 2010-07-12 08:00:00.000 98765 2012-04-13 10:00:00.000 2012-04-26 16:00:00.000 2012-04-13 10:00:00.000 2012-04-26 16:00:00.000
プレ>最後の 2 つの出力列は、
outer apply
の結果です。これは次の設定に基づいています:
declare @gap int set @gap = 30 set dateformat dmy -----P_ID----|----starttime---- declare @SOURCETABLE table (P_ID int, starttime datetime) insert @SourceTable values (12121,'24-03-2009 7:30'), (12121,'24-03-2009 14:25'), (12345,'27-06-2011 10:00'), (12345,'27-06-2011 10:30'), (12345,'28-06-2011 11:00'), (98765,'13-04-2012 10:00'), (12345,'21-07-2011 9:00'), (12345,'21-09-2011 12:00'), (45454,'12-07-2010 8:00'), (12345,'21-09-2011 17:00'), (98765,'26-04-2012 16:00'), (12345,'07-07-2012 14:00'), (12345,'13-08-2012 13:00')
プレ>更新:少し考え直します。 CTE を使用して、各アイテムの前方および後方のギャップを計算し、それらを集計します:
--Get the gap between each starttime and the next and prev (use 999 to indicate non-closed intervals) ;WITH CTE_Gaps As ( select p_id, src.starttime, nextgap = coalesce(DATEDIFF(dd,src.starttime,nxt.starttime),999), --Gap to the next entry prevgap = coalesce(DATEDIFF(dd,prv.starttime,src.starttime),999), --Gap to the previous entry isold = case when DATEDIFF(dd,src.starttime,getdate()) > @gap then 1 else 0 end --Is starttime more than gap days ago? from @SOURCETABLE src cross apply (select starttime = MIN(starttime) from @SOURCETABLE sub where src.p_id = sub.p_id and sub.starttime > src.starttime) nxt cross apply (select starttime = max(starttime) from @SOURCETABLE sub where src.p_id = sub.p_id and sub.starttime < src.starttime) prv ) --select * from CTE_Gaps select p_id, starttime = min(gap.starttime), endtime = nxt.starttime from CTE_Gaps gap --Find the next starttime where its gap to the next > @gap cross apply (select starttime = MIN(sub.starttime) from CTE_Gaps sub where gap.p_id = sub.p_id and sub.starttime >= gap.starttime and sub.nextgap > @gap) nxt group by P_ID, nxt.starttime order by P_ID, nxt.starttime
プレ>