I have a table with users, a month series, a flag for eligibility, a running sum of an amount, and an incremental amount column (example data below). I need to find the amount that has accumulated for each user since the last eligibility flag has appeared.
User | Month | Eligibility | RunningAmt | new Amt |
---|---|---|---|---|
1 | Jan 20 | 1 | 0 | 0 |
1 | Feb 20 | 0 | 150 | 150 |
1 | Mar 20 | 0 | 150 | 0 |
1 | Apr 20 | 1 | 1000 | 850 |
1 | May 20 | 0 | 1200 | 200 |
1 | Jun 20 | 0 | 1200 | 0 |
1 | Jul 20 | 1 | 1200 | 0 |
1 | Aug 20 | 0 | 1500 | 300 |
1 | Sep 20 | 0 | 1550 | 50 |
1 | Oct 20 | 0 | 1600 | 50 |
1 | Nov 20 | 1 | 1600 | 0 |
1 | Dec 20 | 1 | 1600 | 0 |
create table example (
user int,
month_series int,
eligibility int,
running_amt int,
new_amt int
);
insert into example
(user, month_series, eligibility, running_amt, new_amt)
values
(1,1,1,0,0),
(1,2,0,150,150),
(1,3,0,150,0),
(1,4,1,1000,850),
(1,5,0,1200,200),
(1,6,0,1200,0),
(1,7,1,1200,0),
(1,8,0,1500,300),
(1,9,0,1550,50),
(1,10,0,1600,50),
(1,11,1,1600,0),
(1,12,1,1600,0);
I have tried many kinds of lag() functions but can't seem to get anything to work. What I want is a column that resets to zero when the eligibility flag is 1 and adds the new amounts in between, essentially:
User | Month | Eligibility | RunningAmt | new Amt | desired result |
---|---|---|---|---|---|
1 | Jan 20 | 1 | 0 | 0 | 0 |
1 | Feb 20 | 0 | 150 | 150 | 0 |
1 | Mar 20 | 0 | 150 | 0 | 0 |
1 | Apr 20 | 1 | 1000 | 850 | 1000 |
1 | May 20 | 0 | 1200 | 200 | 0 |
1 | Jun 20 | 0 | 1200 | 0 | 0 |
1 | Jul 20 | 1 | 1200 | 0 | 200 |
1 | Aug 20 | 0 | 1500 | 300 | 0 |
1 | Sep 20 | 0 | 1550 | 50 | 0 |
1 | Oct 20 | 0 | 1600 | 50 | 0 |
1 | Nov 20 | 1 | 1600 | 0 | 400 |
1 | Dec 20 | 1 | 1600 | 0 | 0 |