I have a table that is something like this:
| amount | status | timestamp |
|---|---|---|
| 10 | A | 0 |
| 10 | B | 1 |
| 15 | B | 2 |
| 10 | C | 3 |
| 12 | D | 4 |
| 20 | A | 5 |
| 25 | B | 6 |
| 17 | C | 7 |
| 19 | D | 8 |
The amounts have no restriction (other than being a number). And status lines can have duplicates (the 'B' in the example).
What I want is to sum over everything between 'A' status. So the result should be
| sum | timestamp |
|---|---|
| 57 | 1 |
| 81 | 5 |
I need this for ansi-sql (Spark)
Once you will have decided about your "order" column, one possible solution:
Note that replacing "when 'A' then lv else" by "when 'A' then null else" will give you the sum of rows strictly between the 2 'A' (not including the first one).