WITH t AS ( SELECT '10057' no, TO_DATE('2021-10-18 09:13:16', 'yyyy-mm-dd hh24:mi:ss') dt FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 09:28:27', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 09:52:38', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 09:55:22', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 09:57:54', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 09:59:47', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 10:01:35', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 10:03:16', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 10:05:12', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 10:08:27', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 10:12:31', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 10:27:50', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 11:05:49', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 11:14:22', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 12:46:51', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 12:48:55', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 12:50:57', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 13:43:42', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 14:25:33', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 14:25:48', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:18:26', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:19:52', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:20:31', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:25:44', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:26:50', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:28:54', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:30:46', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:37:27', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:44:47', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 15:59:17', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 21:27:45', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 21:30:05', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 21:32:53', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 21:55:26', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 21:57:51', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL SELECT '10057', TO_DATE('2021-10-18 22:00:47', 'yyyy-mm-dd hh24:mi:ss') FROM dual ) , t1 AS ( SELECT no, dt , ROW_NUMBER() OVER(PARTITION BY no ORDER BY dt) rn FROM t ) , t2 (no, dt, rn, seq1, seq2, dt_1) AS ( SELECT no , dt , rn , 1 seq1 , 1 seq2 , dt dt_1 FROM t1 WHERE rn = 1 UNION ALL SELECT b.no , b.dt , b.rn , CASE WHEN b.dt - a.dt_1 <= 1/24/60 * 30 THEN a.seq1 ELSE a.seq1 + 1 END seq1 , CASE WHEN b.dt - a.dt_1 <= 1/24/60 * 30 THEN a.seq2 + 1 ELSE 1 END seq2 , CASE WHEN b.dt - a.dt_1 <= 1/24/60 * 30 THEN a.dt_1 ELSE b.dt END dt_1 FROM t2 a , t1 b WHERE b.no = a.no AND b.rn = a.rn + 1 ) SELECT no , dt , seq1 , seq2 FROM t2 ;
SELECT no , dt , DENSE_RANK() OVER(PARTITION BY no ORDER BY dt_1) seq1 , ROW_NUMBER() OVER(PARTITION BY no, dt_1 ORDER BY dt ) seq2 FROM (SELECT * FROM t MODEL PARTITION BY (no) DIMENSION BY (ROW_NUMBER() OVER(PARTITION BY no ORDER BY dt) rn) MEASURES (dt, dt dt_1) RULES (dt_1[rn > 1] = CASE WHEN dt[cv()] - dt_1[cv()-1] <= 1/24/60 * 30 THEN dt_1[cv()-1] ELSE dt[cv()] END) ) ;