我需要在'失敗'事件之間獲得'生產'事件總和。這有點難以解釋,所以讓我們看看數據以及所需的結果。見下表。輸入表格也作爲底部的文本給出。SQL Server查詢 - 事件之間的事件時間總和
黃色的項目是失敗事件(代碼100)。綠色的項目是生產事件(代碼200)。我需要在失敗事件之間的生產時間,如上表所示。請注意,最後一個失敗事件之後沒有任何生產事件,因此需要忽略它。
我的猜測是Rank()函數會涉及某個地方,但我正在爲這個而苦惱!
+---------------------+---------------------+-----------------+------------+-----------+
| StartDate | EndDate | DurationInHours | Equipment | EventCode |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-01 06:31:51 | 2014-01-01 09:14:57 | 2.7183 | Equipment1 | 100 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-01 09:17:20 | 2014-01-01 13:34:40 | 4.2889 | Equipment1 | 200 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-01 21:59:49 | 2014-01-01 23:20:29 | 1.3444 | Equipment1 | 200 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-03 22:23:33 | 2014-01-03 22:41:57 | 0.3067 | Equipment1 | 100 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-03 22:51:49 | 2014-01-04 05:48:43 | 6.9483 | Equipment1 | 200 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-04 06:26:18 | 2014-01-04 14:04:20 | 7.6339 | Equipment1 | 200 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-04 14:32:44 | 2014-01-04 18:07:29 | 3.5792 | Equipment1 | 200 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-04 18:27:16 | 2014-01-04 22:40:37 | 4.2225 | Equipment1 | 200 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-04 22:40:37 | 2014-01-04 23:13:15 | 0.5439 | Equipment1 | 100 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-04 23:13:15 | 2014-01-04 23:13:20 | 0.0014 | Equipment1 | 200 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-04 23:13:26 | 2014-01-05 07:24:11 | 8.1792 | Equipment1 | 200 |
+---------------------+---------------------+-----------------+------------+-----------+
| 2014-01-05 07:24:11 | 2014-01-05 09:24:45 | 2.0094 | Equipment1 | 100 |
+---------------------+---------------------+-----------------+------------+-----------+
請不要發佈代碼,樣本數據爲圖像,幫助我們幫助你,而不是圖像發佈爲文本格式。 – Sami
我在底部創建了一個文本表格 - 對不起, – user1035217