WITH changes AS ( SELECT "DATE", CASE WHEN LAG( "DATE" ) OVER ( ORDER BY "DATE" ) + INTERVAL '5' MINUTE ="DATE" THEN 0 ELSE 1 END AS has_changed_group FROM TEST ), grps AS ( ИЗБЕРЕТЕ "ДАТА", SUM( has_changed_group) OVER ( ПОРЪЧКА ПО "ДАТА" РЕДОВЕ МЕЖДУ НЕОГРАНИЧЕН ПРЕДИШЕН И ТЕКУЩ РЕД ) AS grp FROM промени) SELECT MIN( "ДАТА" ) AS shutdown_start, MAX( "ДАТА" ) AS shutdown_end, MAX( "DATE" ) - MIN( "DATE" ) + ИНТЕРВАЛ '5' МИНУТА AS shutdown_durationFROM grpsGROUP BY grp;
Изход:
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.0.01000:000 код>
Редактиране - Множество машини:
Настройка на схема на Oracle 11g R2 :
CREATE TABLE TEST ( MACHINE_ID, "DATE" ) AS SELECT 1, TIMESTAMP '2015-07-07 12:05:00' FROM DUALUNION ALL SELECT 1, TIMESTAMP '2015-07-07 12:10:00' ОТ DUALUNION ALL SELECT 1, TIMESTAMP '2015-07-07 12:15:00' ОТ DUALUNION ALL SELECT 1, TIMESTAMP '2015-07-07 12:35:00' ОТ DUALUNION ALL SELECT 1, TIMESTAMP '2015- 07-07 12:40:00' ОТ DUALUNION ВСИЧКИ ИЗБЕРЕТЕ 1, ЧАСОВ КЛАПЕТ '2015-07-07 12:45:00' ОТ DUALUNION ВСИЧКИ ИЗБЕРЕТЕ 1, ЧАСОВ КЛАПЕТ '2015-07-07 12:50:00' ОТ DUALUNION ВСИЧКИ ИЗБЕРЕТЕ 1, ЧАСОВ КЛАПЕТ '2015-07-07 13:05:00' ОТ DUALUNION ВСИЧКИ ИЗБЕРЕТЕ 1, ЧАСОВ КЛАПЕТ '2015-07-07 13:10:00' ОТ DUALUNION ВСИЧКИ ИЗБЕРЕТЕ 1, ЧАСОВ КЛАПЕТ '2015-07-07 13:15:00' ОТ DUALUNION ALL SELECT 2, TIMESTAMP '2015-07-07 12:35:00' ОТ DUALUNION ALL SELECT 2, TIMESTAMP '2015-07-07 12:40:00' ОТ DUALUNION ALL SELECT 2, TIMESTAMP ' 2015-07-07 12:45:00' ОТ DUALUNION ALL SELECT 2, ЧАСОВ КЛАПЕТ '2015-07-07 13:00:00' ОТ DUALUNION ALL SELECT 2, ЧАСОВ КЛАПЕТ '2015-07-07 13:05:00' ОТ DUALUNION ALL SELECT 2, TIMESTAMP ' 2015-07-07 13:10:00' ОТ DUALUNION ВСИЧКИ ИЗБЕРЕТЕ 2, ВРЕМЕННО КЛАПО '2015-07-07 13:15:00' ОТ DUAL;
Заявка 1 :
С промени AS ( SELECT MACHINE_ID, "DATE", CASE WHEN LAG( "DATE" ) OVER (PARTITION BY MACHINE_ID ORDER BY "DATE" ) + INTERVAL '5' MINUTE ="DATE" THEN 0 ELSE 1 END AS has_changed_group FROM TEST), grps AS ( SELECT MACHINE_ID, "DATE", SUM( has_changed_group) OVER ( PARTITION BY MACHINE_ID ORDER BY "DATE" ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS grp FROM changes)SELECT MACHINE_ID, TO_CHAR( MIN ( "ДАТА"), 'ГГГГ-ММ-ДД HH24:MI:SS' ) AS shutdown_start, TO_CHAR( MAX( "ДАТА"), 'YYYY-MM-DD HH24:MI:SS' ) AS shutdown_end, TO_CHAR( MAX ( "ДАТА" ) - МИН( "ДАТА" ) + ИНТЕРВАЛ '5' МИНУТА ) КАТО shutdown_durationFROM grpsGROUP BY MACHINE_ID, grpORDER BY 1,2
<предварителен код>| 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 |