2012-09-24 50 views
0

我有一個大型數據庫表,其中包含由startstop時間描述的時間段。簡單的時間跨度具有優先權,時間跨度可能相互重疊。在SQL服務器中按優先級裁剪重疊時間段

我需要處理它,以便重疊將被刪除。
在重疊的情況下,具有較高優先級的跨度將優先,而具有較低優先級的時間跨度將被裁剪,以使兩者不重疊。 如果時間跨度被一個或多個具有更高優先級的時間片完全重疊,則應該將其移除。

一個簡單的示例表:

SELECT 
    1 AS id, 
    {ts '2012-09-24 10:00:00'} AS start, 
    {ts '2012-09-24 11:00:00'} AS stop, 
    10 AS priority 
INTO #TABLE 
UNION ALL SELECT 2, {ts '2012-09-24 10:15:00'}, {ts '2012-09-24 12:00:00'}, 5 
UNION ALL SELECT 3, {ts '2012-09-24 10:30:00'}, {ts '2012-09-24 12:30:00'}, 1 
UNION ALL SELECT 4, {ts '2012-09-24 11:30:00'}, {ts '2012-09-24 13:00:00'}, 15 

SELECT * FROM #TABLE; 
DROP TABLE #TABLE; 

應導致:

Start    Stop    Priority 
2012-09-24 10:00 2012-09-24 11:00 10 
2012-09-24 11:00 2012-09-24 11:30 5 
2012-09-24 11:30 2012-09-24 13:00 15 

這是可能的,但我無法找到任何簡單的解決它。最好我想避免使用遊標。但是,如果沒有其他方式,那麼它就是遊標。

回答

2

嘗試

;with cte as 
(select start as timepoint from @table union select stop from @table) 
,cte2 as (select *, ROW_NUMBER() over (order by timepoint) rn from cte) 

    select id, MIN(ts) as starttime, max(te) as stoptime, maxpri 
    from @table t2 
     inner join 
     (    
     select ts, te, MAX(priority) as maxpri 
     from @table t1 
      inner join 
      (  
      select c1.rn, c1.timepoint as ts, c2.timepoint as te 
      from cte2 c1 
      inner join cte2 c2 on c1.rn+1 = c2.rn 
      ) v 
      on t1.start<v.te and t1.stop>v.ts 
     group by ts, te 
     ) v 
      on t2.priority = v.maxpri 
      and ts>=start and te<=stop 
     group by id, maxpri 
     order by starttime 
+0

我認爲你是對的東西。但是,目前代碼不會返回預期的結果。行ID = 2將被刪除,而不是裁剪到時間戳11:00 - 11:30,其他更高優先級的時間段沒有重疊。我會分析你的代碼,看看它是否可以糾正。 – ANisus

+0

@ANisus你確定嗎? 10:00-11:00 10,11:00-11:30 5,11:30-13:00 15如預期的那樣? – podiluska

+0

podiluska的解決方案適用於我。 –