2017-10-28 114 views
0

採樣數據不返回所有行: enter image description here的MS Access:左聯接在左表

SELECT tblStudent.student_id, 
     [monthly_fee]+[book_fee] AS [Total Fee], 
     Sum(tblReceipt.receipt_amount) AS SumOfreceipt_amount, 
     [monthly_fee]+[book_fee]-Nz(Sum([tblReceipt]![receipt_amount]),0) AS Outstanding, 
     tblReceipt.month, 
     tblReceipt.year 
FROM tblStudent 
LEFT JOIN tblReceipt ON tblStudent.student_id = tblReceipt.student_id 
GROUP BY tblStudent.student_id, 
     [monthly_fee]+[book_fee], 
     tblReceipt.Description, 
     tblReceipt.month, 
     tblReceipt.year 
HAVING (((Sum(tblReceipt.receipt_amount))>0) 
     AND ((tblReceipt.Description)='Total Fee') 
     AND (([monthly_fee]+[book_fee]-Nz(Sum([tblReceipt]![receipt_amount]),0))>0) 
     AND ((tblReceipt.month)=[Forms]![frmDialogMonth3]![cb_Month]) 
     AND ((tblReceipt.year)=[Forms]![frmDialogMonth3]![txt_Year])) 
OR (((Sum(tblReceipt.receipt_amount)) IS NULL)) 
ORDER BY tblStudent.student_id; 

嗨,

請找到數據連接的樣品臺及以上的SQL語句。 我想創建一份報告,向那些未付費的學生和那些未支付全額費用(描述=總費用)的那些學生顯示當月的未付款項。

我得到的結果只顯示沒有全額支付未付金額的學生,以及在tblReceipt中沒有任何記錄的學生。 tblReceipt記錄中除總費用之外的那些學生沒有出現在結果中。

請在下面找到樣品結果。

Result

回答

1

你不能做tblReceipt任何過濾如果左加入。

您需要首先建立一個查詢返回的總費用收據中的相關一個月每個學生:

SELECT student_id, Sum(receipt_amount) AS FeeReceipts 
FROM tblReceipt 
WHERE [description]='Total Fee' AND [month]=[Forms]![frmDialogMonth3]![cb_Month] AND [year]=[Forms]![frmDialogMonth3]![txt_Year] 
GROUP BY student_id; 

我把它叫做MonthFeeReceipts。然後在主查詢中使用:

SELECT tblStudent.student_id, monthly_fee, book_fee, FeeReceipts, [monthly_fee]+[book_fee]-Nz([FeeReceipts],0) AS Outstanding 
FROM tblStudent LEFT JOIN MonthFeeReceipts ON tblStudent.student_id = MonthFeeReceipts.student_id; 
+0

嗨馬克,非常感謝您的幫助。欣賞它! – Chong

+0

嗨。如果它確實有效,那麼請將其標記爲答案。謝謝馬克 –