Skip to content
Advertisement

Sum of columns with same name in PostgreSQL

I have a table with a structure like this:

I want the amount and total columns to be added according to the name column (I need the op column to keep appearing). For this example, my select would return this result:

Can I do this using PostgreSQL?

Advertisement

Answer

Create schema:

SELECTstatement:

Result:

op name amount total
1 a 120 6
2 a 120 6
5 b 80 12
6 b 80 12
1 c 50 5
3 c 50 5

Test it here: db<>fiddle

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