Skip to content
Advertisement

How to use select statement inside SUM function

I have a table which looks like this.

I want to execute sum query on this. I have tried

This query is working fine but its summing balance to 2500 but as ID 4 & 5 are of same Flat so I want it to sum latest of balance which should be 1500. I have tried to put a select statement inside sum function but that doesn’t work . So how can I achieve this?

Advertisement

Answer

You can select the most recent id for each block/flats combo first (using row_number()) and then aggregate:

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