Reputation: 2124
I am struggling to calculate a column (c3) when querying my table. in fact the value of c3 for a current row is depend on all previous value of the (c3).
for example the value of c3 of raw 3 will depend on c3 of raw2 and this latter is depended on c3 of raw1, and so on ...
| c1 | c2 | c3 |
|----|-------|----------|
| 1 | 1 | |<-- row1
| 2 | 6 | |<-- row2
| 3 | 2 | |<-- row3
| 4 | 10 | |<-- row4
how could I write my query.
Upvotes: 0
Views: 79
Reputation: 222432
That's a window sum. Assuming that column c1
defines the ordering of the rows, that would be:
select
c1,
c2,
sum(c1 + c2) over(order by c1) c3
from mytable
Upvotes: 2