SQLite错误 - 滥用聚合:sum()

时间:2016-03-05 15:39:05

标签: android mysql sql sqlite

您好我滥用了聚合:以后查询的sum()错误。这有什么不对。

> Select storeid, supplierid, ordernumber, inwardnumber, returnid,
> taxinvoiceno, ordervalue, sum(CASHPAID) as 'CASHPAID', balancedue,
> paidamt, paiddate, sum(ADJUSTMENT) as
> ADJUSTMENT,ordervalue+sum(ADJUSTMENT) as total from
> supplierpaymentstabledb where storeid = 10008 and total!=CASHPAID
> group by inwardnumber

1 个答案:

答案 0 :(得分:3)

作为一种良好实践,您应该将所有非聚合列放在group by子句中。然后,聚合列的条件应该是having子句,而不是where子句:

Select storeid, supplierid, ordernumber, inwardnumber, returnid,
       taxinvoiceno, ordervalue, sum(CASHPAID) as CASHPAID, balancedue,
       paidamt, paiddate, sum(ADJUSTMENT) as ADJUSTMENT, 
       ordervalue + sum(ADJUSTMENT) as total
from supplierpaymentstabledb
where storeid = 10008 
group by storeid, supplierid, ordernumber, inwardnumber, returnid,
         taxinvoiceno, ordervalue, balancedue,
         paidamt, paiddate
having total <> CASHPAID;