MySQL Multiple Subquery on same table

Posted by user1444980 on Stack Overflow See other posts from Stack Overflow or by user1444980
Published on 2012-06-08T16:21:57Z Indexed on 2012/06/08 16:40 UTC
Read the original article Hit count: 162

Filed under:

I have a table of the following structure

ID | Amount | Bank (1 or 2)
---+--------+------
1  | 100000 | 1
2  | 256415 | 2
3  | 142535 | 1
1  | 214561 | 2
2  | 123456 | 1
1  | 987654 | 2

I want a result like this (from the same table):

ID | sum(Bank 1) | sum(Bank 2)
---+-------------+------------
1  | 100000      | 1202215
2  | 123456      | 256415
3  | 142535      | 0

What will be the easiest query to achieve this?

© Stack Overflow or respective owner

Related posts about mysql