I have a transfer table which looks like this:
I would like to get the balance of each participant from the transfer table.
The output of the query should looks like this:
Advertisement
Answer
You could try with something like this:
x
select sender,(select sum(amount)
from table1 b
where b.recipient=a.sender)-sum(amount)
from table1 a
group by sender
Where you get for every sender the sum of the amount he has given as a sender minus the sum of the amount he has got as receiver.
EDIT Some one told me that the sum was inverted and he was right, so I changed the order of the operands