I have a table like this :
CREATE TABLE `hoststatus` (
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`host` varchar(100) NOT NULL,
`result` tinyint(1) NOT NULL,
PRIMARY KEY (`timestamp`,`host`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `hoststatus` (`timestamp`, `host`, `result`) VALUES
('2012-06-08 14:18:01', 'host1', 0),
('2012-06-08 14:19:01', 'host1', 0),
('2012-06-08 14:20:01', 'host1', 1),
('2012-06-08 14:21:01', 'host1', 1),
('2012-06-08 14:22:01', 'host1', 1),
('2012-06-08 14:23:01', 'host1', 1),
('2012-06-08 14:24:01', 'host1', 0),
('2012-06-08 14:25:01', 'host1', 0),
('2012-06-08 14:26:01', 'host1', 0),
('2012-06-08 14:27:01', 'host1', 0),
('2012-06-08 14:28:01', 'host1', 1),
('2012-06-08 14:29:01', 'host1', 1),
('2012-06-08 14:29:04', 'host2', 0),
('2012-06-08 14:30:01', 'host1', 0),
('2012-06-08 14:30:03', 'host2', 0),
('2012-06-08 14:31:01', 'host1', 0),
('2012-06-08 14:32:01', 'host1', 0),
('2012-06-08 14:32:02', 'host2', 1),
('2012-06-08 14:33:01', 'host1', 0);
This contains connection status of some hosts : 1 means “offline” and 0 is “online”. What I want to deduce using a select request, is the begin and the end timestamps of each “offline” status of each host. In the example above, the request should return something like this :
begin end host
'2012-06-08 14:20:01' '2012-06-08 14:23:01' 'host1'
'2012-06-08 14:28:01' '2012-06-08 14:29:01' 'host1'
'2012-06-08 14:32:02' '2012-06-08 14:32:02' 'host2'
can someone give me ways to fix this please? Thank you.
You are essentially trying to find “runs” or “streaks” in your data. This answer may provide some guidance for you on how to accomplish your task.
EDIT: The linked answer can be translated to the following for your specific issue: