【问题标题】:MS Access: Left Join does not return all the rows in the left tableMS Access:左连接不返回左表中的所有行
【发布时间】:2017-10-28 10:22:52
【问题描述】:

样本数据:

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 中的记录不是 Total Fee 的学生没有出现在结果中。

请在下面找到示例结果。

Result

【问题讨论】:

    标签: sql ms-access


    【解决方案1】:

    如果 tblReceipt 是 Left Joined,则不能对它进行任何过滤。

    您需要首先构建一个查询以返回每个学生在相关月份的总费用收据:

    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;
    

    【讨论】:

    • 嗨。如果它成功了,那么请将其标记为答案。谢谢马克
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2012-08-29
    • 2020-12-17
    相关资源
    最近更新 更多