I have a SQL Server table that contains some data and I need to do some complex equations to get some result in same table
The table structure and data like the following:
A B C D E
-------------------------------------------------
5 2015 / 2016 0 0 0
7 2017 / 2018 0 24000 0
8 2018 / 2019 0 24000 0
9 2019 / 2020 0 26000 0
10 2020 / 2021 36000 0 0
I need to update
E9 = IF C10 > D9 = D9
IF C10 < D9 = C10
IF C10 = D9 = C10 or D9
E8 = IF C10 - E9 > D8 = (C10-E9) - D8
IF C10 - E9 < D8 = C10-E9
IF C10 - E9 = D8 = C10-E9 or D8
And so on till to reach to first row.
The result should be
A B C D E
-------------------------------------------------
5 2015 / 2016 0 0 0
7 2017 / 2018 0 24000 0
8 2018 / 2019 0 24000 10000
9 2019 / 2020 0 26000 26000
10 2020 / 2021 36000 0 0
Column E Sum should equal C10.