我需要计算事件组之间的时间差。每个“事件组”在event_count
column = 1 时重新开始,最大值event_count
可以是大于 1 的任何数字。
我怎样才能在 MySQL 中做到这一点?
这是表格和一些测试数据:
桌子
CREATE TABLE `monitoring` (
`event_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`hosting_id` char(36) DEFAULT NULL,
`event_timestamp` datetime DEFAULT NULL,
`event_type` tinyint(3) unsigned DEFAULT NULL,
`event_count` int(6) DEFAULT NULL,
`session` varchar(36) DEFAULT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1765 DEFAULT CHARSET=utf8;
测试记录:
INSERT INTO `monitoring` (`event_id`, `hosting_id`, `event_timestamp`, `event_type`, `event_count`, `session`)
VALUES
(1753,'97948a60-2e44-d39c-bb02-506c80c97df4','2013-10-23 20:01:19',1,1,NULL),
(1711,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:02:20',1,1,NULL),
(1712,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:02:26',1,2,NULL),
(1713,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:02:30',1,3,NULL),
(1714,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:02:33',1,4,NULL),
(1715,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:02:45',1,5,NULL),
(1716,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:02:48',1,6,NULL),
(1717,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:02:51',1,7,NULL),
(1718,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:29',1,1,NULL),
(1719,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:31',1,2,NULL),
(1720,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:33',1,3,NULL),
(1721,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:35',1,4,NULL),
(1722,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:38',1,5,NULL),
(1723,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:40',1,6,NULL),
(1724,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:43',1,7,NULL),
(1725,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:46',1,8,NULL),
(1726,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:48',1,9,NULL),
(1727,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 16:49:50',1,10,NULL),
(1754,'988df609-4ae4-3062-6361-4fd2665cfb42','2013-10-24 02:48:32',1,1,NULL),
(1729,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:02:28',1,1,NULL),
(1730,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:03:25',1,2,NULL),
(1731,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:03:30',1,3,NULL),
(1732,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:05:42',1,4,NULL),
(1733,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:06:03',1,5,NULL),
(1758,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-24 12:23:43',1,1,NULL),
(1757,'d425f99b-0812-5902-37da-4fd266c64545','2013-10-24 08:16:26',1,1,NULL),
(1756,'20e00e08-459b-b229-d341-4fd266c23df1','2013-10-24 06:32:10',1,1,NULL),
(1738,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:09:02',1,1,'5c31a1eb481bf9e0753f9a25538b0257'),
(1739,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:09:04',1,2,'5c31a1eb481bf9e0753f9a25538b0257'),
(1740,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:09:06',1,3,'5c31a1eb481bf9e0753f9a25538b0257'),
(1741,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:09:07',1,4,'5c31a1eb481bf9e0753f9a25538b0257'),
(1742,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:09:08',1,5,'5c31a1eb481bf9e0753f9a25538b0257'),
(1743,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:09:09',1,6,'5c31a1eb481bf9e0753f9a25538b0257'),
(1755,'988df609-4ae4-3062-6361-4fd2665cfb42','2013-10-24 06:30:20',1,1,NULL),
(1745,'a9e17a2a-42d9-979f-0484-4fd2665b487a','2013-10-23 17:09:28',1,1,'5c31a1eb481bf9e0753f9a25538b0257'),
(1746,'a9e17a2a-42d9-979f-0484-4fd2665b487a','2013-10-23 17:09:38',1,2,'5c31a1eb481bf9e0753f9a25538b0257'),
(1747,'a9e17a2a-42d9-979f-0484-4fd2665b487a','2013-10-23 17:09:55',1,3,'5c31a1eb481bf9e0753f9a25538b0257'),
(1748,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:13:54',1,1,NULL),
(1749,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:13:56',1,2,NULL),
(1750,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:13:58',1,3,NULL),
(1751,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-23 17:14:00',1,4,NULL),
(1752,'ecd5f06a-7b82-4cc8-deb2-4fd266eeec3e','2013-10-23 19:06:42',1,1,NULL),
(1759,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-24 12:23:48',1,2,NULL),
(1760,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-24 12:23:51',1,3,NULL),
(1761,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-24 12:23:53',1,4,NULL),
(1762,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-24 12:23:56',1,5,NULL),
(1763,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-24 12:23:58',1,6,NULL),
(1764,'c47204d5-d682-9ba4-b8b6-4fd26660651a','2013-10-24 12:24:23',1,7,NULL);
我的目标是:
On 10-23-2013 down for 31 seconds
On 10-23-2013 down for 21 seconds
On 10-23-2013 down for 215 seconds
etc...
一旦我能正确地弄清楚这个查询,我就会这样做。
到目前为止,我的查询是:
SELECT min(date(`event_timestamp`)) as EventGroupDate, (max(`event_timestamp`) - min(`event_timestamp`)) HowLong
FROM (
SELECT `event_timestamp`, @eventGroup := @eventGroup + (`event_count` = 1) eventGroup
FROM `monitoring`, (SELECT @eventGroup := 0) init
WHERE `hosting_id` = 'c47204d5-d682-9ba4-b8b6-4fd26660651a'
ORDER BY `event_timestamp`
) s
GROUP BY eventGroup
但计算错误。具体计算如下
我会在制作时发布更新。
每个事件组从event_count
= 1 开始,并且可以以大于 1 的任何数字结束。由于这需要按 排序,event_timestamp
因此可以假设介于两者之间的所有事件都可以视为一个组。例如:记录 ID 1711-1717 将是组 1,1718-1727 将是组 2,依此类推...
确切的结果应该是:
31
21
215
7
6
40
使用给定的数据