Skip to content
Advertisement

Calculate Number of Consecutive Days Where a Condition Applies Across Two Columns

I have a table similar to below:

I need to calculate the number of consecutive days where “Balance <0” for reach ID and for each distinct Date (including the date itself in the calculation). Each Id might have several balances in a given date either positive amount or negative. Even if one balance amount in a given day is negative, the query should take that day into account. The output result should be similar to the table below:

Could you please suggest me a way to calculate that? It is highly appreciated.

Advertisement

Answer

Note that any gaps between dates will be treated as consecutive days.

To treat missing dates as nonconsecutive try:

https://rextester.com/NKBZG48737

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