2016-04-27 62 views
-1

我有以下格式的數據並希望以第二種格式輸出它。我已經嘗試了自己的數據和組的自我加入ID,時間和事件,但沒有我試過似乎正常工作。查詢以獲得每個組的最大時間

EV-2的時間是開始時間,EV-5的時間是結束時間。 EV-5時間可以寫爲每個EV-2-EV-5事件的結束時間。

感謝您對此事的任何指導。

ID TIME      EVENT 
1 2016-03-21 12:30:00.000  EV-1  
1 2016-03-21 12:30:30.000  EV-2 
1 2016-03-21 12:33:00.000  EV-3 
1 2016-03-21 12:33:00.000  EV-4 
1 2016-03-21 12:33:45.000  EV-5 
1 2016-03-21 12:33:50.000  EV-1  
1 2016-03-21 12:33:55.000  EV-2 
1 2016-03-21 12:35:15.000  EV-3 
1 2016-03-21 12:35:15.000  EV-4 
1 2016-03-21 12:40:20.000  EV-5 
2 2016-03-21 12:40:50.000  EV-1  
2 2016-03-21 12:41:25.000  EV-2 
2 2016-03-21 12:41:25.000  EV-3 
2 2016-03-21 12:42:13.000  EV-4 
2 2016-03-21 12:47:35.000  EV-5 

Output 
ID TIME(startTime)    EVENT endTime 
1 2016-03-21 12:30:00.000  EV-1  
1 2016-03-21 12:30:30.000  EV-2 2016-03-21 12:33:45.000 
1 2016-03-21 12:33:00.000  EV-3 2016-03-21 12:33:45.000 
1 2016-03-21 12:33:00.000  EV-4 2016-03-21 12:33:45.000 
1 2016-03-21 12:33:45.000  EV-5 2016-03-21 12:33:45.000 
1 2016-03-21 12:33:50.000  EV-1  
1 2016-03-21 12:33:55.000  EV-2 2016-03-21 12:40:20.000 
1 2016-03-21 12:35:15.000  EV-3 2016-03-21 12:40:20.000 
1 2016-03-21 12:35:15.000  EV-4 2016-03-21 12:40:20.000 
1 2016-03-21 12:40:20.000  EV-5 2016-03-21 12:40:20.000 
2 2016-03-21 12:40:50.000  EV-1  
2 2016-03-21 12:41:25.000  EV-2 2016-03-21 12:47:35.000 
2 2016-03-21 12:41:25.000  EV-3 2016-03-21 12:47:35.000 
2 2016-03-21 12:42:13.000  EV-4 2016-03-21 12:47:35.000 
2 2016-03-21 12:47:35.000  EV-5 2016-03-21 12:47:35.000 

我已經試過:

select d.ID, d.TIME, d.EVENT, max(dd.TIME) endTime 
from dataTable d 
    inner join dataTable dd on d.ID = dd.ID and d.EVENT = dd.EVENT and d.TIME < dd.TIME 
group by d.ID, d.TIME, d.EVENT 
+3

什麼是你的DBMS? – Siyual

+3

你可以顯示你的查詢嗎? –

+0

選擇d.ID,d.TIME,d.EVENT,最大值(dd.TIME)從dataTable中d ENDTIME 內上d.ID = dd.ID和 d.EVENT = dd.EVENT和 加入的dataTable DD d.TIME

回答

0

簡單的解決方案,使用相關子選擇返回最小的EV-5時間大於或等於當前行的時間。 (有一個case表達對所有事件,但EV-1這樣做)

select t1.*, case when t1.event <> 'EV-1' then (select min(t2.TIME) from dataTable t2 
               where t2.event = 'EV-5' 
                and t2.TIME >= t1.time) 
      end as endTime 
from dataTable t1 

另外,如果id可以使用,做一個自我(左)參加:

select t1.*, case when t1.event <> 'EV-1' then t2.TIME 
      end as endTime 
from dataTable t1 
    left join dataTable t2 on t1.id = t2.id and t2.event = 'EV-5' 

left join到也返回的ID沒有任何EV-5,如果需要的話)

+0

它看起來事件是通過ID鏈接... – dotjoe

+0

@dotjoe,很好的觀察。錯過了細節! – jarlh

0

@ jarhl的邏輯的DBMS支持如SQL Server 2014窗聚合函數:

SELECT t.*, 
    CASE 
    WHEN EVENT <> 'EV-1' 
    THEN MIN(CASE WHEN EVENT = 'EV-5' THEN TIME end) 
      OVER (PARTITION BY ID -- maybe not needed 
       ORDER BY TIME 
       ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) 
    END AA endTime 
FROM tablename AS t1 
+0

謝謝!你的解決方案工作得很好,正是我所期待的。 –

0

糾正你的ID列到實際ID,嘗試

IF (SELECT OBJECT_ID('tempdb..#Events')) IS NOT NULL 
BEGIN 
    DROP TABLE #Events 
END 

CREATE TABLE #Events (ID INT,EventTime datetime, eventName varchar(4)) 
INSERT INTO #Events 
VALUES   
(1, '2016-03-21 12:30:00.000','EV-1'), 
(1, '2016-03-21 12:30:30.000','EV-2'), 
(1, '2016-03-21 12:33:00.000','EV-3'), 
(1, '2016-03-21 12:33:00.000','EV-4'), 
(1, '2016-03-21 12:33:45.000','EV-5'), 
(2, '2016-03-21 12:33:50.000','EV-1'), 
(2, '2016-03-21 12:33:55.000','EV-2'), 
(2, '2016-03-21 12:35:15.000','EV-3'), 
(2, '2016-03-21 12:35:15.000','EV-4'), 
(2, '2016-03-21 12:40:20.000','EV-5'), 
(3, '2016-03-21 12:40:50.000','EV-1'), 
(3, '2016-03-21 12:41:25.000','EV-2'), 
(3, '2016-03-21 12:41:25.000','EV-3'), 
(3, '2016-03-21 12:42:13.000','EV-4'), 
(3, '2016-03-21 12:47:35.000','EV-5') 
; 
WITH cteEvent AS 
(
    SELECT ID, EventTime AS startTime,en.endTime 
    FROM #Events e 
    CROSS APPLY (
        SELECT EventName,EventTime AS endTime 
        FROM #Events e2 
        WHERE e2.ID = e.id AND CAST(RIGHT(e2.eventname,1) AS INT) = 5) as en 
    WHERE e.EventName = 'EV-1' 
) 
SELECT c.id,eventName,startTime, 
    CASE 
     WHEN eventName = 'EV-1' 
     THEN NULL 
     ELSE endTime 
    END AS endTime 
FROM cteEvent c 
LEFT JOIN #Events e ON c.id = e.id 
相關問題