Find overdrawn accounts in SQL
- by mazzzzz
Hey guys, I have a program that allows me to run queries against a large database.
I have two tables that are important right now, Deposits and withdraws. Each contains a history of every user. I need to take each table, add up every deposit and withdraws (per user), then subtract the withdraws from the deposits. I then need to return every user whos result is negative (aka they withdrew more then they deposited).
Is this possible in one query?
Example:
Deposit Table:
|ID|UserName|Amount|
|1 | Use1 |100.00|
|2 | Use1 |50.00 |
|3 | Use2 |25.00 |
|4 | Use1 | 5.00 |
WithDraw Table:
|ID|UserName|Amount|
|2 | Use2 | 5.00 |
|1 | Use1 |100.00|
|4 | Use1 | 5.00 |
|3 | Use2 |25.00 |
So then the result would output:
|OverWithdrawers|
| Use2 |
Is this possible (I sure don't know how to do it)?
Thanks for any help,
Max