Hi Bala,
OLAP functions are probably not the right approach (i tried them on a similar problem a few years ago and didn't find an efficient solution), but this can be easily solved (if the number of rows per a/b combination is not too large) using recursion:
CREATE VOLATILE TABLE vt (a NOT NULL, b NOT NULL, rn NOT NULL, start_time NOT NULL) AS
(
SELECT
a,b, ROW_NUMBER() OVER (PARTITION BY a,b ORDER BY start_time) AS rn, start_time
FROM tab
) WITH DATA UNIQUE PRIMARY INDEX (a,b, rn) ON COMMIT PRESERVE ROWS
;
WITH RECURSIVE cte (rn, a, b, start_time, end_time) AS
(
SELECT rn, a, b, start_time,
start_time + INTERVAL '23:59:59' HOUR TO SECOND AS end_time
FROM vt
WHERE rn=1
UNION ALL
SELECT
vt.rn, vt.a, vt.b, vt.start_time,
CASE WHEN vt.start_time > cte.end_time THEN vt.start_time + INTERVAL '23:59:59' HOUR TO SECOND ELSE cte.end_time END
FROM vt JOIN cte
ON vt.a = cte.a AND vt.b = cte.b AND vt.rn = cte.rn+1
)
SELECT * FROM cte
ORDER BY start_time
;
DROP TABLE vt;Dieter
↧