2013-06-13 84 views
0

我在Access數據庫中有兩個表[Party]和[Invoice]。我想總結所有的派對餘額。這裏是我的查詢到目前爲止:用於顯示更新的帳戶餘額的SQL查詢

select 
    Party.AccountCode, 
    Party.AccountDescription, 
    OpeningBalance+sum(invoiceAmount) as balance, 
    invoiceDate 
from Party,Inovoice 
where party.accountCode=Inovice.AccountCode 
    and invoiceDate>=01-01-2013 
    and invoiceDate<=30-06-2013 
group by 
    Party.AccountCode, 
    Party.AccountDescription, 
    invoiceDate 

該查詢返回只出現在[發票]表中的雙方的平衡:TOM和JHONS。我想顯示的餘額爲所有4個國家:

enter image description here

回答

0

使用兩個查詢。首先計算總髮票金額與第二黨餘額

qrySumInvoiceAmounts:

SELECT 
AccountCode 
sum(invoiceAmount) as InvoiceBalance, 
FROM Invoice  
group by 
AccountCode, 
WHERE invoiceDate>= #01-01-2013# 
and invoiceDate<= #30-06-2013# 

qryPartyBalance:

SELECT 
Party.AccountCode, 
Party.AccountDescription, 
OpeningBalance + InvoiceBalance, 
from Party LEFT JOIN qrySumInvoiceAmounts 
ON party.accountCode=qrySumInvoiceAmounts.AccountCode 
+0

這個查詢告訴你試圖在一個錯誤並不inculde指定expersion opensBalance + sum(invoiceAmount)作爲agregate函數的一部分 –

+0

查詢工作但它不會爲沒有發票的帳戶帶來任何結果 –

+0

嘗試將總金額(invoiceAmount)更改爲nz(總和(invoiceAmount),0) –