Find overdrawn accounts in SQL

Posted by mazzzzz on Stack Overflow See other posts from Stack Overflow or by mazzzzz
Published on 2010-06-05T23:17:32Z Indexed on 2010/06/06 0:12 UTC
Read the original article Hit count: 258

Filed under:

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

© Stack Overflow or respective owner

Related posts about sql