Skip to content
Advertisement

How to sum up and other calculations by the other columns in the same table?

id amount
1 20
2 20
3 20
4 30
5 100
6 20
7 30
8 100

I would like to create three more columns which will be calculated by the amount column.

Expected output:

id amount openamt debamt credamt closeamt
1 20 340 140 200 400
2 20 340 140 200 400
3 20 340 140 200 400
4 30 340 140 200 400
5 100 340 140 200 400
6 20 340 140 200 400
7 30 340 140 200 400
8 100 340 140 200 400

What I tried, I got it working for openamt but not for the rest. Can somebody point out me to the right direction, please?

Advertisement

Answer

You were in the right area. You can use a conditional aggregation within the window function sum() over()

Example

Results

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement