2014-12-13 176 views
0

我一直在嘗試做一個SUM與內部連接,但它顯示每個單元格而不是將它們添加在一起,並只顯示在一個單元格中,我在哪裏出錯?內部連接總和SQL

SELECT SUM(trans) as 'Transactions',city as 'city' 
FROM account 
INNER JOIN branch 
ON branch.bID 
GROUP BY account.trans, branch.city; 

回答

1

單獨添加branch.city使用下面的查詢,將每個城市返回總和

SELECT SUM(trans) as 'Transactions', branch.city as 'city' 
FROM account 
INNER JOIN branch 
ON branch.bID = account.bID 
GROUP BY branch.city; 
+0

啊,謝謝你能看到我現在出錯的地方。 – 2014-12-13 15:48:32

+0

不客氣。不要忘記通過接受答案來解決這個問題,如果你認爲這樣做有幫助,並且通過讓其他人從中受益。 – SMA 2014-12-13 15:50:02

+0

是的,我會告訴我等幾分鐘來接受答案。 – 2014-12-13 15:52:15

1

Group by刪除account.trans。由於您在group by中添加了account.trans, branch.city,因此它會顯示sum(trans)transcity

如果你想sum(trans)city然後在group by

SELECT SUM(trans) as 'Transactions' 
FROM account 
INNER JOIN branch 
ON branch.bID 
Group by branch.city; 
+0

啊對不起,我重讀我的問題,它很清楚,每個城市都應該有自己的總量。 – 2014-12-13 15:47:01

+0

@JonSnow - 已更新現在檢查 – 2014-12-13 15:48:36