MySQL Forums
Forum List  »  Newbie

Re: Retrieving a count & sum of contiguous rows?
Posted by: Peter Brawley
Date: July 27, 2011 10:44AM

Computing time in SQL is most always dogged and slow. This problem's no exception. Here's a 4-step solution:

-- add in a sequential ID to simplify processing:
drop table if exists t;
create table t(ord int primary key auto_increment,id int,site char(8),status char(8),dt datetime);
insert into t (id,site,status,dt) values
(151519, 'testsite', 'success', '2009-01-26 15:45:00'),
(151520, 'testsite', 'success', '2009-01-26 15:47:00'),
(151521, 'testsite', 'success', '2009-01-26 15:49:00'),
(151529, 'testsite', 'success', '2009-01-26 15:51:00'),
(151530, 'testsite', 'success', '2009-01-26 15:53:00'),
(151540, 'testsite', 'success', '2009-01-26 15:55:00'),
(151541, 'testsite', 'failure', '2009-01-26 15:57:00'),
(151542, 'testsite', 'failure', '2009-01-26 15:59:00'),
(151558, 'testsite', 'failure', '2009-01-26 16:01:00'),
(151559, 'testsite', 'success', '2009-01-26 16:03:00'),
(151569, 'testsite', 'failure', '2009-01-26 16:05:00'),
(151570, 'testsite', 'success', '2009-01-26 16:07:00'),
(151571, 'testsite', 'success', '2009-01-26 16:09:00'),
(151579, 'testsite', 'success', '2009-01-26 16:11:00'),
(151580, 'testsite', 'failure', '2009-01-26 16:13:00'),
(151598, 'testsite', 'failure', '2009-01-26 16:15:00'),
(151599, 'testsite', 'success', '2009-01-26 16:17:00'),
(151600, 'testsite', 'success', '2009-01-26 16:19:00'),
(151608, 'testsite', 'success', '2009-01-26 16:21:00'),
(151609, 'testsite', 'success', '2009-01-26 16:23:00');

-- write fail sequence start times to temp table
drop temporary table if exists a;
set @outage=0;
create temporary table a
select @outage:=@outage+1 as outage,a.ord 
from      t a
left join t b on a.ord=b.ord+1 
where a.status='failure' and (b.ord is null or b.status='success')
order by a.ord;

-- write fail sequence end times to another temp table
drop temporary table if exists b;
set @outage=0;
create temporary table b
select @outage:=@outage+1 as outage,a.ord 
from      t a
left join t b on a.ord=b.ord-1 
where a.status='failure' and (b.ord is null or b.status='success')
order by a.ord;

-- write results
select a.outage, c.site,c.dt as start,concat(2+timestampdiff(minute,c.dt,d.dt), ' mins') as duration
from a
join b using(outage)
join t as c on a.ord=c.ord
join t as d on b.ord=d.ord;
+--------+----------+---------------------+----------+
| outage | site     | start               | duration |
+--------+----------+---------------------+----------+
|      1 | testsite | 2009-01-26 15:57:00 | 6 mins   |
|      2 | testsite | 2009-01-26 16:05:00 | 2 mins   |
|      3 | testsite | 2009-01-26 16:13:00 | 4 mins   |
+--------+----------+---------------------+----------+

PB
http://www.artfulsoftware.com

Options: ReplyQuote


Subject
Written By
Posted
Re: Retrieving a count & sum of contiguous rows?
July 27, 2011 10:44AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.