内连接和SQL

时间:2014-12-13 15:42:47

标签: sql sum

我一直在尝试使用内部联接进行SUM,但它会显示每个单元格,而不是将它们一起添加并仅显示在一个单元格中,我哪里出错?

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

2 个答案:

答案 0 :(得分:1)

删除account.trans中的Group by。由于您在account.trans, branch.city中添加了group by,因此每sum(trans)trans会显示city

如果您希望每sum(trans) city,请在branch.city

中单独添加group by
SELECT SUM(trans) as 'Transactions'
FROM account 
INNER JOIN branch 
ON branch.bID 
Group by branch.city;

答案 1 :(得分:1)

使用以下查询将返回每个城市的总和

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