oracle - SQL grouping on time interval -


i have data set based on timestamp. data set present record on every shut down occurrence in 5 minute time interval. if shut down occurred in specific 5 min, record added else no record. no record means system has recovered

     date           07-jul-15 12:05:00  07-jul-15 12:10:00 07-jul-15 12:15:00 07-jul-15 12:35:00 07-jul-15 12:40:00 07-jul-15 12:45:00 07-jul-15 12:50:00 07-jul-15 13:05:00 07-jul-15 13:10:00 07-jul-15 13:15:00  

i query , return

1.number of shutdowns: number of shut down in case 3 based on between

12:15 12:35  12:50 13:05 

the system recovered

  1. period between every shut down

example:

1.from: 07-jul-15 12:05:00 to: 07-jul-15 12:15:00 duration : 15 mins

2.from: 07-jul-15 12:35:00 to: 07-jul-15 12:50:00 duration : 20 mins

there similar question although different solution required one.

would appreciate fiddle example

with changes (   select "date",          case when lag( "date" ) on ( order "date" ) + interval '5' minute = "date" 0 else 1 end has_changed_group     test ), grps (   select "date",          sum( has_changed_group ) on ( order "date" rows between unbounded preceding , current row ) grp     changes ) select min( "date" ) shutdown_start,        max( "date" ) shutdown_end,        max( "date" ) - min( "date" ) + interval '5' minute shutdown_duration   grps group grp; 

output:

shutdown_start               shutdown_end                 shutdown_duration ---------------------------- ---------------------------- ----------------- 07-jul-15 12.05.00.000000000 07-jul-15 12.15.00.000000000 0 0:15:0.0         07-jul-15 12.35.00.000000000 07-jul-15 12.50.00.000000000 0 0:20:0.0         07-jul-15 13.05.00.000000000 07-jul-15 13.15.00.000000000 0 0:15:0.0   

edit - multiple machines:

sql fiddle

oracle 11g r2 schema setup:

create table test ( machine_id, "date" )           select 1, timestamp '2015-07-07 12:05:00' dual union select 1, timestamp '2015-07-07 12:10:00' dual union select 1, timestamp '2015-07-07 12:15:00' dual union select 1, timestamp '2015-07-07 12:35:00' dual union select 1, timestamp '2015-07-07 12:40:00' dual union select 1, timestamp '2015-07-07 12:45:00' dual union select 1, timestamp '2015-07-07 12:50:00' dual union select 1, timestamp '2015-07-07 13:05:00' dual union select 1, timestamp '2015-07-07 13:10:00' dual union select 1, timestamp '2015-07-07 13:15:00' dual union select 2, timestamp '2015-07-07 12:35:00' dual union select 2, timestamp '2015-07-07 12:40:00' dual union select 2, timestamp '2015-07-07 12:45:00' dual union select 2, timestamp '2015-07-07 13:00:00' dual union select 2, timestamp '2015-07-07 13:05:00' dual union select 2, timestamp '2015-07-07 13:10:00' dual union select 2, timestamp '2015-07-07 13:15:00' dual; 

query 1:

with changes (   select machine_id,          "date",          case when lag( "date" ) on ( partition machine_id order "date" ) + interval '5' minute = "date" 0 else 1 end has_changed_group     test ), grps (   select machine_id,          "date",          sum( has_changed_group ) on ( partition machine_id order "date" rows between unbounded preceding , current row ) grp     changes ) select machine_id,        to_char( min( "date" ), 'yyyy-mm-dd hh24:mi:ss' ) shutdown_start,        to_char( max( "date" ), 'yyyy-mm-dd hh24:mi:ss' ) shutdown_end,        to_char( max( "date" ) - min( "date" ) + interval '5' minute ) shutdown_duration   grps group machine_id, grp order 1,2 

results:

| machine_id |      shutdown_start |        shutdown_end |             shutdown_duration | |------------|---------------------|---------------------|-------------------------------| |          1 | 2015-07-07 12:05:00 | 2015-07-07 12:15:00 | +000000000 00:15:00.000000000 | |          1 | 2015-07-07 12:35:00 | 2015-07-07 12:50:00 | +000000000 00:20:00.000000000 | |          1 | 2015-07-07 13:05:00 | 2015-07-07 13:15:00 | +000000000 00:15:00.000000000 | |          2 | 2015-07-07 12:35:00 | 2015-07-07 12:45:00 | +000000000 00:15:00.000000000 | |          2 | 2015-07-07 13:00:00 | 2015-07-07 13:15:00 | +000000000 00:20:00.000000000 | 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -