2015-06-16 64 views
0

這裏之間時間戳差值是我與在MS Access從系統跟蹤處理的數據時,代理使得系統的變化:ACCESS:計算行

|agentid|eventtype|reasoncode|eventdatetimelocal | 
|1830 |2  |32762  |01/01/2014 7:11:44 PM| 
|1830 |3  |0   |01/01/2014 7:13:46 PM| 
|1830 |2  |32762  |01/01/2014 7:14:55 PM| 
|1833 |2  |0   |01/01/2014 7:11:35 PM| 
|1833 |3  |32762  |01/01/2014 7:13:25 PM| 

我需要確定在它們之間經過的秒數行代理。我還想保留事件類型和原因代碼的細節。

我試圖加入一個subqry,但它不工作:

SELECT sub1.agentid, 
     sub1.eventtype, 
     sub1.reasoncode, 
     sub1.eventdatetimelocal, 
     (sub1.next_timestamp-sub1.eventdatetimelocal) AS duration 
FROM (SELECT i.agentid, 
     eventdatetimelocal, 
     eventtype, 
     reasoncode, (SELECT 
         Min([eventdatetimelocal]) 
        FROM state_detail_tbl 
        WHERE [eventdatetimelocal] > i.eventdatetimelocal 
        ) AS next_timestamp 
    FROM state_detail_tbl AS i 
    WHERE i.eventdatetimelocal BETWEEN #01/01/2014# AND #01/31/2014# 
) AS sub1; 
+0

您的樣本數據的預期輸出是什麼? – Gustav

+0

我期望沿着這樣的方向行事:agentid,eventype1,reasoncode1,eventdatetimelocal1,eventdatetimelocal2,eventtype2,reasoncode2,秒。通過這種方式,我可以獲得衝頭,衝出,事件和原因代碼以及持續時間。 – rynsmns

回答

0

我從查詢接收到錯誤下面沿着「這一行,說明什麼查詢只能返回最多一行「。但是查詢和這個參考文獻:Calculating time difference between activity timestamps in a query給了我我需要的東西,下面列出來供參考。我決定我的最初需求太寬泛了,所以我簡化了查詢以返回將時間戳帶到上一行所需的最小數據。我可以用另一個查詢來計算秒數。這需要一段時間才能處理,但它的工作原理可以在需要時運行一夜。

SELECT i.agentid, i.eventtype, i.reasoncode, eventdatetimelocal, (SELECT 
Min([eventdatetimelocal]) FROM state_detail_subqry WHERE agentid = i.agentid 
AND [eventdatetimelocal]>i.[eventdatetimelocal]) AS next_timestamp 
FROM state_detail_subqry AS i 
ORDER BY agentid, eventdatetimelocal; 
0

你可以試試這個查詢

SELECT sub1.agentid, 
     sub1.eventtype, 
     sub1.reasoncode, 
     sub1.eventdatetimelocal, 
     (SELECT TOP 1 sub2.eventdatetimelocal - sub1.eventdatetimelocal 
     FROM state_detail_tbl AS sub2 
     WHERE sub1.agentid=sub2.agentid 
       AND sub2.eventdatetimelocal > sub1.eventdatetimelocal 
     ORDER BY sub2.eventdatetimelocal) AS duration 

FROM state_detail_tbl sub1 
WHERE (SELECT TOP 1 eventdatetimelocal 
     FROM state_detail_tbl AS s3 
     WHERE sub1.agentid=s3.agentid 
      AND s3.eventdatetimelocal > sub1.eventdatetimelocal) Is Not Null 
AND sub1.eventdatetimelocal BETWEEN #01/01/2014# AND #01/31/2014# 
ORDER BY sub1.agentid, sub1.eventdatetimelocal;