Hi Feroz,
you need some recursive process like this:
CREATE VOLATILE TABLE vt AS
(
SELECT customerid,
entrytimestamp,
ROW_NUMBER() OVER (PARTITION BY customerid ORDER BY entrytimestamp) AS rn
FROM feroz
) WITH DATA PRIMARY INDEX (customerid, rn)
ON COMMIT PRESERVE ROWS
;
WITH RECURSIVE cte (customerid, entrytimestamp, prevTS, valid_ind, rn) AS
(
SELECT customerid, entrytimestamp, entrytimestamp, 1 AS valid_ind, 1 AS rn
WHERE rn = 1
FROM vt
UNION ALL
SELECT vt.customerid, vt.entrytimestamp,
CASE WHEN cte.prevTS < ADD_MONTHS(vt.entrytimestamp, -6) THEN vt.entrytimestamp ELSE cte.prevTS END,
CASE WHEN cte.prevTS < ADD_MONTHS(vt.entrytimestamp, -6) THEN 1 ELSE 0 END,
vt.rn
FROM vt JOIN cte
ON vt.customerid = cte.customerid AND vt.rn = cte.rn+1
)
SELECT * FROM cte
ORDER BY 1,2
;
Btw, 2011-06-15 is less than 6 months from 2011-01-01 :-)
Dieter
↧