0

Table A

Week    Column_1   Column_2
0       9          92
1       0          84
2       1          84
3       4          83

The result I want is

Week Column_1  Column_2  Remaining
0    9         92        83
1    0         84        83
2    1         84        82
3    4         83        78

So, if you notice. I want to calculate the Remaining as Column_2 - Column_1 for week 0 and after week 0 I want to calculate the Remaining as Remaining - Column_1

Is it possible?

bharath
  • 49
  • 1
  • 7
  • Possible duplicate of [Running Total by Group SQL (Oracle)](https://stackoverflow.com/questions/22155401/running-total-by-group-sql-oracle) – XING Oct 20 '17 at 13:46

1 Answers1

1

You can use this query.

SELECT a.*,
       SUM (CASE
              WHEN a.week = 0 THEN column_2 - column_1
              ELSE -column_1
            END)
         over (
           ORDER BY week )
FROM   tablea a;  
Kaushik Nayak
  • 30,772
  • 5
  • 32
  • 45