我在下面有一个简单的查询,它基本上过滤了每个分支的储蓄帐户。
SELECT Account.bID as Branch,branch.bAddress.street as Street, branch.bAddress.city as City, branch.bAddress.postcode as Postcode, Account.accType as Type
FROM Account
INNER JOIN branch
ON Account.bID=branch.bID where accType='savings';
我正在尝试添加计数,因此不显示列表,而是显示每个分支的储蓄帐户金额。我不太确定我在哪里添加这个特定查询的计数。
感谢任何帮助。
答案 0 :(得分:1)
SELECT branch.bid, branch.bAddress.street as Street,
branch.bAddress.city as City, branch.bAddress.postcode as Postcode,
count(account.*) as savings_count
FROM Account
INNER JOIN branch
ON Account.bID=branch.bID where accType='savings'
group by branch.bid, branch.bAddress.street,
branch.bAddress.city, branch.bAddress.postcode;
答案 1 :(得分:1)
只需在count(*)
和select
中添加group by
:
SELECT a.bID as Branch, b.bAddress.street as Street, b.bAddress.city as City,
b.bAddress.postcode, a.accType as Type, COUNT(*)
FROM Account a INNER JOIN
branch b
ON a.bID = b.bID
WHERE accType = 'savings'
GROUP BY a.bID, b.bAddress.street, b.bAddress.city, b.bAddress.postcode;
这已经用别名替换了表名,因此更容易阅读。但是,某些列(例如b.bAddress.street
)不是有效的SQL。