LEFT加入不返回MS Access中的记录

时间:2013-07-20 07:09:41

标签: c# sql ms-access left-join

我最近的项目我有要求打印应收款摘要。我需要从OpeningBalance表返回整行,并匹配VoucherHeaderCustomers中的行。

我的SQL查询是这个

SELECT 
    OpeningBalance.OpenID, Sum(OpeningBalance.Amount) AS SumOfAmount,  
    Sum(VoucherHeader.Debit) AS SumOfDebit, Sum(VoucherHeader.Credit) AS SumOfCredit, 
    Customers.CustomerID, Customers.CustomerName 
FROM 
   (OpeningBalance 
LEFT OUTER JOIN 
   VoucherHeader ON OpeningBalance.OpenID = VoucherHeader.LedgerID) 
INNER JOIN 
   Customers ON OpeningBalance.OpenID = Customers.CustomerID
WHERE 
   (((Customers.CustomerType)='Debtor') 
     AND ((VoucherHeader.VoucherDate)<#2013/06/02#))
GROUP BY 
    OpeningBalance.OpenID, Customers.CustomerID, Customers.CustomerName, 
    VoucherHeader.LedgerID
ORDER BY 
    Customers.CustomerName;

请帮忙。

1 个答案:

答案 0 :(得分:0)

SELECT OB.OpenID, Sum(OB.Amount) AS SumOfAmount,  
Sum(VB.Debit) AS SumOfDebit, Sum(VB.Credit) AS SumOfCredit, CS.CustomerID,
CS.CustomerName FROM OpeningBalance OB 
LEFT OUTER JOIN VoucherHeader VB ON OB.OpenID = VB.LedgerID
LEFT OUTER JOIN Customers CS ON OB.OpenID =   CS.CustomerID
WHERE (((CS.CustomerType)='Debtor') AND ((VB.VoucherDate)<#2013/06/02#))
GROUP BY OB.OpenID, CS.CustomerID, CS.CustomerName, VB.LedgerID
ORDER BY CS.CustomerName;