简体   繁体   中英

How is the query to SUM joined tables?

I don't know how the title should be. Thinking about the title takes more time than writing this question.
to the point
Let's assume I have three tables:

//table customers 
|  ID  | Name                  |
++++++++++++++++++++++++++++++++
| 194  | PT Comro Haneut       |
| 195  | PT Kareueut Kameumeut |

//table customer's savings 
|  ID  | IDCustomer | SavingsAmount |
+++++++++++++++++++++++++++++++++++++
|   1  |    194     |    5000000    |
|   2  |    195     |     250000    |
|   3  |    195     |    2500000    |
|   4  |    194     |     125000    |
|   5  |    194     |     175000    |

//table transactions
|  ID  | IDCustomer | Amount        |
+++++++++++++++++++++++++++++++++++++
|  1   |    195     |    1000000    |
|  2   |    195     |     250000    |
|  3   |    194     |    3500000    |
|  4   |    194     |     300000    |

The Goal
I want to sum the amount of savings and the amount of transaction, and make the result in one line, like this :

// expected result of the query
| IDCustomer | Savings      | Transactions  | Balance       |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|   194      |    5300000   |   4800000     |    500000     |
|   195      |    2750000   |   1250000     |   1500000     |

I tried to build the query by my self, but I always failed. I got the total of the amount of savings and transaction was doubled.

Can anyone help ?

query

select svg.id, Savings, Transactions, (Savings - Transactions) as Balance
from
(
  select c.id as id, sum(s.SavingsAmount) as Savings
  from customers c
  inner join savings s
  on c.id=s.idcustomer
  group by c.id
) svg
inner join
(
  select c.id as id, sum(t.amount) as Transactions
  from customers c
  inner join transactions t
  on c.id=t.idcustomer
  group by c.id
) trans
on svg.id = trans.id
;

setup

create table customers
(
  id integer primary key not null,
  name varchar(23) not null
);

create table savings
(
  id integer primary key not null,
  IDCustomer integer not null,
  SavingsAmount decimal(10, 2) not null,
  foreign key ( IDCustomer ) references customers ( id )
);

create table transactions
(
  id integer primary key not null,
  IDCustomer integer not null,
  amount decimal(10, 2) not null,
  foreign key ( IDCustomer ) references customers ( id )
);

insert into customers
( id, name )
values
( 194  , 'PT Comro Haneut'       ),
( 195  , 'PT Kareueut Kameumeut' )
;

insert into savings 
(  id  , IDCustomer , SavingsAmount )
values
(   1  ,    194     ,    5000000    ),
(   2  ,    195     ,     250000    ),
(   3  ,    195     ,    2500000    ),
(   4  ,    194     ,     125000    ),
(   5  ,    194     ,     175000    )
;

insert into transactions
(  id  , IDCustomer , amount        )
values
(  1   ,    195     ,    1000000    ),
(  2   ,    195     ,     250000    ),
(  3   ,    194     ,    3500000    ),
(  4   ,    194     ,     300000    )
;

output

+-----+------------+--------------+------------+
| id  | Savings    | Transactions | Balance    |
+-----+------------+--------------+------------+
| 194 | 5300000.00 |   3800000.00 | 1500000.00 |
| 195 | 2750000.00 |   1250000.00 | 1500000.00 |
+-----+------------+--------------+------------+

sqlfiddle

Looks you don't need any specific data from customers table, so just to optimize the result you can use

SELECT savings.IDCustomer, sum(SavingsAmount) as savings,sum(Amount) as amount, sum(SavingsAmount)-sum(Amount) as Balance
FROM savings
LEFT JOIN transactions
ON savings.IDCustomer=transactions.IDCustomer
group by savings.IDCustomer
ORDER BY savings.IDCustomer;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM