Skip to content
Advertisement

Complicated PostgreSQL query to group by multiple columns

I have PostgreSQL table with client_id, date, smp_number, vs_type, vs_amount:

What I want to achieve is to group by client_id and month and then to get the total debit sum and the total credit sum by month in distinct columns and the last smp_number in each month.

The result should be:

Hope this makes sense, any help is appreciated. I have searched all the relevant topics here.

Advertisement

Answer

Your sample data does not produce your expected output, but something like this should work.

dbfiddle.uk

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