我正在使用以下 SQL:
SELECT
a.name as accountName,
SUM((i.value - i.paidValue)) as oneMonth
FROM insertions i
JOIN contracts c ON i.contractId = c.id
JOIN accounts a ON c.accountId = a.id
WHERE i.value > i.paidValue
AND i.isDeleted is FALSE
AND i.isCanceled is FALSE
AND i.month = 8
AND i.year = 2013
它应该产生如下输出:
accountName | oneMonth
CompanyA | 3000 [ which is a sum of 1000, 1000, 1000 ]
CompanyB | 1000
但它正在做的是:
accountName | oneMonth
CompanyA | 4000
我究竟做错了什么?