Advertisement

05.22.2007 at 03:29PM PDT, ID: 22588702
[x]
Attachment Details

LEFT OUTER JOIN on multiple tables

Asked by TSFLLC in MS SQL Server

Tags: join, outer, left, multiple, tables

I'm apparently confused about the way SQL works with a LEFT OUTER JOIN.  Below is a sample statement I am using for summing InvAmt, PmtAmt, CreditMemoAmt.  Payments and credits are based on a join of Invoices.

SELECT a.contact_id, a.property_id, a.invoice_id,
MAX(a.invoice_amount) AS InvoiceAmt,
CASE WHEN SUM(pmt.payment_amount) IS NULL THEN 0 ELSE SUM(pmt.payment_amount) END AS PmtAmt,
CASE WHEN SUM(cm.credit_memo_amount_applied) IS NULL THEN 0 ELSE SUM(cm.credit_memo_amount_applied) END AS CMAmt
FROM accounting_link_invoice AS a
LEFT OUTER JOIN accounting_link_payment AS pmt ON a.contact_id = pmt.contact_id AND a.property_id = pmt.property_id
           AND a.invoice_id = pmt.invoice_id AND pmt.accounting_date <= CONVERT(datetime, '05/22/2007')
LEFT OUTER JOIN accounting_link_credit AS cm ON a.contact_id = cm.contact_id AND a.property_id = cm.property_id
           AND a.invoice_id = cm.invoice_id AND cm.accounting_date <= CONVERT(datetime, '05/22/2007')
WHERE (a.property_id = 921) AND (a.accounting_date <= CONVERT(datetime, '05/22/2007')) AND (a.contact_id = 929)
GROUP BY a.contact_id, a.property_id, a.invoice_id

All of my numbers come out correct except for ONE invoice where there are BOTH payments and credits associated with it.  There are (3) payment records and one credit_memo record.  The sum of CMAmt = 3 * credit_memo_amount_applied instead of 1 * credit_memo_amount_applied.

What do I have to do with this select statement to get an independent sum of payments and independent sum of credits for each invoice??Start Free Trial
[+][-]05.22.2007 at 03:40PM PDT, ID: 19138119

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.22.2007 at 04:02PM PDT, ID: 19138220

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.22.2007 at 04:27PM PDT, ID: 19138318

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.22.2007 at 04:28PM PDT, ID: 19138329

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.22.2007 at 04:36PM PDT, ID: 19138370

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.22.2007 at 04:41PM PDT, ID: 19138389

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.22.2007 at 04:43PM PDT, ID: 19138396

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.22.2007 at 04:52PM PDT, ID: 19138434

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.22.2007 at 07:17PM PDT, ID: 19138956

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.23.2007 at 12:43PM PDT, ID: 19144378

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: join, outer, left, multiple, tables
Sign Up Now!
Solution Provided By: ScottPletcher
Participating Experts: 3
Solution Grade: A
 
 
[+][-]05.23.2007 at 01:29PM PDT, ID: 19144737

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32