デバイスが障害コードで開始または停止したときのイベントのログがあり、障害と開始の間の平均時間と平均時間を計算しようとしています。以下は、非常に単純なデータ テーブルの例です。
+----+-----------+---------------------+
| id | eventName | eventTime |
+----+-----------+---------------------+
| 1 | start | 2012-11-01 14:25:20 |
| 2 | fail A | 2012-11-01 14:27:45 |
| 3 | start | 2012-11-01 14:30:49 |
| 4 | fail B | 2012-11-01 14:32:54 |
| 5 | start | 2012-11-01 14:35:59 |
| 6 | fail A | 2012-11-01 14:37:02 |
| 7 | start | 2012-11-01 14:38:05 |
| 8 | fail A | 2012-11-01 14:40:09 |
| 9 | start | 2012-11-01 14:41:11 |
| 10 | fail C | 2012-11-01 14:43:14 |
+----+-----------+---------------------+
コードを作成します。
CREATE TABLE `test` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`eventName` varchar(50) NOT NULL,
`eventTime` datetime NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `test` (`id`, `eventName`, `eventTime`) VALUES (1,'start','2012-11-01 14:25:20'),(2,'fail A','2012-11-01 14:27:45'),(3,'start','2012-11-01 14:30:49'),(4,'fail B','2012-11-01 14:32:54'),(5,'start','2012-11-01 14:35:59'),(6,'fail A','2012-11-01 14:37:02'),(7,'start','2012-11-01 14:38:05'),(8,'fail A','2012-11-01 14:40:09'),(9,'start','2012-11-01 14:41:11'),(10,'fail C','2012-11-01 14:43:14');
次のようなものを使用して、開始から失敗までの時間を取得できます。
SET @time_prev := -1;
SELECT
*
FROM
(
SELECT
eventName
, eventTime
, @ts := UNIX_TIMESTAMP(eventTime) AS ts
, @started := IF(eventName = 'start', 1, 0) AS started
, @failed := IF(eventName <> 'start', 1, 0) AS failed
, @time_diff := IF(@time_prev > -1, @ts - @time_prev, 0) AS time_diff
, @time_prev := @ts AS time_prev
, @time_to_fail := IF(@failed, @time_diff, 0) AS time_to_fail
, @time_to_start := IF(@started, @time_diff, 0) AS time_to_start
FROM
test
) AS t1;
+-----------+---------------------+------------+---------+--------+-----------+------------+--------------+---------------+
| eventName | eventTime | ts | started | failed | time_diff | time_prev | time_to_fail | time_to_start |
+-----------+---------------------+------------+---------+--------+-----------+------------+--------------+---------------+
| start | 2012-11-01 14:25:20 | 1351805120 | 1 | 0 | 0 | 1351805120 | 0 | 0 |
| fail A | 2012-11-01 14:27:45 | 1351805265 | 0 | 1 | 145 | 1351805265 | 0 | 145 |
| start | 2012-11-01 14:30:49 | 1351805449 | 1 | 0 | 184 | 1351805449 | 184 | 0 |
| fail B | 2012-11-01 14:32:54 | 1351805574 | 0 | 1 | 125 | 1351805574 | 0 | 125 |
| start | 2012-11-01 14:35:59 | 1351805759 | 1 | 0 | 185 | 1351805759 | 185 | 0 |
| fail A | 2012-11-01 14:37:02 | 1351805822 | 0 | 1 | 63 | 1351805822 | 0 | 63 |
| start | 2012-11-01 14:38:05 | 1351805885 | 1 | 0 | 63 | 1351805885 | 63 | 0 |
| fail A | 2012-11-01 14:40:09 | 1351806009 | 0 | 1 | 124 | 1351806009 | 0 | 124 |
| start | 2012-11-01 14:41:11 | 1351806071 | 1 | 0 | 62 | 1351806071 | 62 | 0 |
| fail C | 2012-11-01 14:43:14 | 1351806194 | 0 | 1 | 123 | 1351806194 | 0 | 123 |
+-----------+---------------------+------------+---------+--------+-----------+------------+--------------+---------------+
しかし、失敗から開始までの時間を確保するために、次のレコードに進み、その失敗コードのグループを失う必要があります。これを次のレベルに移動し、未来から時間の開始を失敗したレコードにマージしてグループ化できるようにするにはどうすればよいですか?
最終的に、平均と中央値を計算すると、次のような結果セットになります。
+-----------+-------------+----------------+--------------+-----------------+
| eventName | avg_to_fail | median_to_fail | avg_to_start | median_to_start |
+-----------+-------------+----------------+--------------+-----------------+
| fail A | 110.66 | 124.00 | 103.00 | 63.00 |
| fail B | 125.00 | 125.00 | 185.00 | 185.00 |
+-----------+-------------+----------------+--------------+-----------------+