求和函数

时间:2016-02-14 10:44:40

标签: mysql

我的SQL查询在对结果上的行求和时#1111 - 无效使用组函数

此查询工作正常并返回许多行。

SELECT credit.Amount - SUM( COALESCE( creditreceived.Amount, 0 ) ) AS 
AmountReceivable FROM credit LEFT JOIN creditreceived ON credit.ID = 
creditreceived.CreditID WHERE credit.CompanyID =  '1' GROUP BY credit.id

结果图片: http://oi65.tinypic.com/qplyli.jpg

我想对这些行求和,为此我正在使用此查询

SELECT SUM( credit.Amount - SUM( COALESCE( creditreceived.Amount, 0 ) ) ) AS
AmountReceivable FROM credit LEFT JOIN creditreceived ON credit.ID = 
creditreceived.CreditID WHERE credit.CompanyID =  '1' GROUP BY credit.id

这是返回错误#1111 - 无效使用群组功能

1 个答案:

答案 0 :(得分:0)

它认为问题不在于群体。尝试

SUM( credit.Amount - COALESCE( creditreceived.Amount, 0 ) ) AS..