2016-03-05 49 views
0

您好我正在濫用follwing查詢的aggregate:sum()錯誤。這有什麼問題。SQLite錯誤 - 錯誤使用aggregate: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 

回答

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; 
+0

我想按inwardnumber只。我可以做嗎? – Seenu69

+0

@ Seenu69在選擇列表中,您可以具有聚合或分組列。我知道的唯一例外是MySQL和他們的「Extended Group By」。如果你想只在一列上組合,另一列必須是聚合。您可以使用min(col)或max(col)作爲其他非分組列(但當然是sum())。 – mauro

+0

@ Seenu69。 。 。是的,但你需要弄清楚你想要的其他欄目(如果你需要的話)。 –