Skip to content
Advertisement

Subtraction of two sums grouped by asset type?

I have a table, like this:

I need to sum all amounts which type is Credit and all amounts which type is Debit for user_id = 1 grouping by asset_type respectively. And then subtract sum of debits from sum of credits for each asset_type. E.g. For example table and user_id = 1 it would be: 100 usd (no debits for this asset type) 50 eur (credit – debit, 100 – 50)

How can i do this in a single query?

Advertisement

Answer

You can use conditional aggregation:

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