I'm in a weird situation with my query. My objective is to display the total deposits and withdrawals from multiple transactions for each person and display them. I am getting multiple rows that I need to collapse into one. This all needs to happen in one query
SELECT
lastname,
firsname,
case when upper(category) = 'W' then sum(abs(principal)) end as Withdrawal,
case when upper(category) = 'D' then sum(abs(principal)) end as Deposit,
description
FROM
table1
JOIN table2 ON table1.id = table2.id
JOIN table3 ON table2.c = table3.c
WHERE
description = 'string'
GROUP BY
lastname,
firstname,
description,
category
my result is
lastname firstname Withdrawal Deposit description
john smith null 140.34 string
john smith 346.00 null string
jane doe null 68.03 string
jane doe 504.00 null string
and I am looking for
lastname firstname Withdrawal Deposit description
john smith 346.00 140.34 string
jane doe 504.00 68.03 string
adding principal into the group does not work. any help on solving this will be greatly appreciated!
Solution using Subquery
Use conditional aggregation . . . the
case
is the argument to thesum()
:Try this: