The set of data that I have has gap between dates and with Lag() function I calculated the number of months that gap exists. Then in second query with dense_rank() I count active Months for each ID. However, as you see in the table the count resets where there is a gap but after,it continues with the previous count instead of the reset number.
Could you please help?
SELECT
GP, ID, Date,
DENSE_RANK() OVER (PARTITION BY ENR.GP, ENR.ID, Age, ENR.DATEDIFF
ORDER BY ENR.DATE ASC) AS Active_MOS
FROM
(SELECT
GP, ID,
MONTHS_BETWEEN (DATE, LAG(DATE, 1) OVER (PARTITION BY GP, ID, CASE WHEN GNDR = 'M' AND AGE < 35 THEN 'YES' ELSE 'NO' END ORDER BY YRMO ASC )) AS DateDiff
FROM
TABLE2)