Skip to content
Advertisement

How to count consecutive dates using Netezza

I need to count consecutive days in order to define my cohorts. I have a table that looks like:

I want such as output:

so that I can use these consecutive days value (per pat_id) to filter for my cohort. I’ve seen few posts that suggested using DateDiff/DateAdd with row_number, such as:

but datediff/dateadd functions wouldn’t work on Netezza…

The closest I’ve got so far was:

which doesn’t recognize gap between dates and return such an output:

Does anyone know how to tackle this?

Advertisement

Answer

Use lag() to see where the groups start and a cumulative sum to define the group. The rest is just row_number():

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement