2015-09-25 52 views
0

我不知道標題應該如何。對標題的思考比寫這個問題需要更多的時間。
給點
假設我有三個表:如何查詢SUM連接表?

//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 | 

目標
我要總結的儲蓄金額和交易量,並且使結果在一行,像這樣:

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

我試圖建立由我自己的查詢,但我總是失敗。我得到的儲蓄和交易總額增加了一倍。

任何人都可以幫忙嗎?

回答

2

查詢

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 
; 

設置

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 ) 
; 

輸出

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

sqlfiddle

+0

就是這樣!子查詢是答案:D –

0

看起來你並不需要從客戶表中的任何具體數據,所以纔來優化你可以使用結果

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; 
+0

是的,我只需要在客戶表中的情況下,如果我要顯示的客戶名稱。但是這個查詢產生與另一個答案相同的結果。 –

+1

同意,結果相同。但是在某處查詢執行時間可以通過避免查詢中不需要的表連接來提高。 – Ranjana