1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | WITH t AS ( SELECT '20131206' yyyymmdd, '2013/12/06 22:00' dttm, 'A' cd, 'start_time' gubun FROM dual UNION ALL SELECT '20131206' , '2013/12/06 22:06' , 'C' , 'end_time' FROM dual UNION ALL SELECT '20131206' , '2013/12/06 22:08' , 'C' , 'start_time' FROM dual UNION ALL SELECT '20131206' , '2013/12/06 22:16' , 'C' , 'start_time' FROM dual UNION ALL SELECT '20131206' , '2013/12/06 22:26' , 'C' , 'start_time' FROM dual UNION ALL SELECT '20131206' , '2013/12/06 22:36' , 'C' , 'end_time' FROM dual UNION ALL SELECT '20131206' , '2013/12/06 22:56' , 'C' , 'start_time' FROM dual UNION ALL SELECT '20131206' , '2013/12/06 23:06' , 'C' , 'end_time' FROM dual ) SELECT yyyymmdd, dttm, cd, gubun , COUNT (DECODE(gubun, 'end_time' , 1)) OVER( ORDER BY dttm ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) + 1 seq FROM t ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | WITH T(yyyymmdd, dttm, cd ,gubun , 순차 ) AS ( SELECT '20131206' , '2013/12/06 22:00' , 'A' , 'start_time' , '1' FROM DUAL UNION ALL SELECT '20131206' , '2013/12/06 22:06' , 'C' , 'end_time' , '1' FROM DUAL UNION ALL SELECT '20131206' , '2013/12/06 22:08' , 'C' , 'start_time' , '2' FROM DUAL UNION ALL SELECT '20131206' , '2013/12/06 22:16' , 'C' , 'start_time' , '2' FROM DUAL UNION ALL SELECT '20131206' , '2013/12/06 22:26' , 'C' , 'start_time' , '2' FROM DUAL UNION ALL SELECT '20131206' , '2013/12/06 22:36' , 'C' , 'end_time' , '2' FROM DUAL UNION ALL SELECT '20131206' , '2013/12/06 22:56' , 'C' , 'start_time' , '3' FROM DUAL UNION ALL SELECT '20131206' , '2013/12/06 23:06' , 'C' , 'end_time' , '3' FROM DUAL ) SELECT T.* , SUM (DECODE(GUBUN, 'end_time' ,1,0)) OVER( ORDER BY DTTM) + 1 - (DECODE(GUBUN, 'end_time' ,1,0)) FROM T |