I have PostgreSQL table with client_id, date, smp_number, vs_type, vs_amount:
client_id |date |smp_number |vs_type |vs_amount
1233455 |2017.05.02 |1234/0123 |debit |10000
1233455 |2017.05.03 |1236/0123 |credit |1000
1233455 |2017.05.04 |1234/0123 |credit |1000
1233455 |2017.05.07 |1235/0123 |debit |15000
1233456 |2017.05.02 |1234/0123 |debit |11000
1233456 |2017.06.03 |1236/0123 |credit |10000
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:
client_id |date |debit_amount |credit_amount |last_smp
1233455 |2017.05.01 |25000 |2000 |1235/0123
1233456 |2017.05.01 |10000 |11000 |1234/0123
1233456 |2017.06.01 |10000 |11000 |1236/0123
Hope this makes sense, any help is appreciated. I have searched all the relevant topics here.