I'm trying to create a query with fields:
- data - Date sequence
- user_id - User ID
- is_paid - Did the user pay this month
- number_of_not_paid - the number of months since the last payment, if the payment is in this month, then 0
select
date,
user_id,
is_paid,
(case when is_paid
then 0
else sum(case when is_paid then 0 else 1 end) over (partition by user_id order by date)
end)
from data
The result I get is:
| date | user_id | is_paid | num |
|---|---|---|---|
| 2020-01-01 | 1 | true | 0 |
| 2020-02-01 | 1 | false | 1 |
| 2020-03-01 | 1 | false | 2 |
| 2020-04-01 | 1 | true | 0 |
| 2020-05-01 | 1 | false | 3 |
| 2020-06-01 | 1 | true | 0 |
And the result I would like to get is:
| date | user_id | is_paid | num |
|---|---|---|---|
| 2020-01-01 | 1 | true | 0 |
| 2020-02-01 | 1 | false | 1 |
| 2020-03-01 | 1 | false | 2 |
| 2020-04-01 | 1 | true | 0 |
| 2020-05-01 | 1 | false | 1 |
| 2020-06-01 | 1 | true | 0 |
How I can fix my query for correct result?
recursivefor this. checkis_paidand if it is false add 1 to the previous value in the same field, else put 0recursivecan help you -- DB Fiddle