0

次の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 

私は何を間違っていますか?

4

2 に答える 2

1

group by 句を忘れた

GROUP BY a.id
于 2013-09-04T15:48:42.340 に答える
0
SELECTa.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
Group by a.name

これは動作します..

于 2013-09-04T15:59:04.020 に答える