Is it possible to lag over (the same) calculated column?
In my sql query I want to calculate a column based on the previous value of that same column plus some other value.
Example: I have columns A B C and D. The value of D can be the following:
- The same value as A
- The previous value of D plus B
- The previous value of D plus C
I tried to use lag to do this but it cannot lag over the same column I am calculating at that time, so
lag(D,1) order by (whatever) as D
will not work.
Right now I am using a package to remember the value, is that the only way?