MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 11th, 2012, 10:20 AM
bamboozled bamboozled is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 bamboozled User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 15 sec
Reputation Power: 0
Joining invoices and payments - seems like a simple problem

Here's a challenge. In the accounts system I've developed I'd like to get a list of outstanding invoices.

I'm trying to do this with three linked tables:

The invoices table has fields including invoicenumber, date, order, customer etc
The invoiceitems table has fields including iteminvoicenumber, description, itemamount etc.
The payments table has fields including paymentinvoicenumber, paymentref, paymentamount etc.

The three tables are joined on the following fields: invoicenumber - iteminvoicenumber - paymentinvoicenumber

For each invoice there may be several invoiceitems and several payments

I'd first like to produce a list which shows the balance of each invoice.

Here's my code which doesn't quite work.

select sum(itemamount)-sum(paymentamount) as invoicebalance, invoicedate, invoicenumber
from invoices
join invoiceitems
on iteminvoicenumber=invoicenumber
join payments
on paymentinvoicenumber=invoicenumber
group by invoicenumber
order by invoicenumber

The problem with this is that if there is more than one invoice item the payment is duplicated for each. I'm not sure if this can be solved by using left/right/inner/outer joins or if something more is needed.

Any help gratefully received!

Thanks

Reply With Quote
  #2  
Old December 11th, 2012, 12:05 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,353 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 4 h 17 m 19 sec
Reputation Power: 4140
here's how to do it by using subqueries to aggregate both the item amounts and the payment amounts
Code:
SELECT i.invoicenumber
     , i.invoicedate 
     , ia.sum_itemamount - p.sum_paymentamounts AS invoicebalance
  FROM invoices AS i
INNER
  JOIN ( SELECT iteminvoicenumber 
              , SUM(itemamount) AS sum_itemamounts
           FROM invoiceitems
         GROUP
             BY iteminvoicenumber ) AS ia
    ON ia.iteminvoicenumber = i.invoicenumber
INNER    
  JOIN ( SELECT paymentinvoicenumber
              , SUM(paymentamount) AS sum_paymentamounts
           FROM payments
         GROUP
             BY paymentinvoicenumber ) AS p
    ON p.paymentinvoicenumber = i.invoicenumber
ORDER 
    BY i.invoicenumber

simple, yes?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old December 11th, 2012, 04:17 PM
bamboozled bamboozled is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 bamboozled User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 15 sec
Reputation Power: 0
Nearly there!

Hi Rudy

Thanks for your swift and brilliantly simple reply!

Reply With Quote
  #4  
Old December 11th, 2012, 04:32 PM
bamboozled bamboozled is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 bamboozled User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 15 sec
Reputation Power: 0
Nearly there!

Hi Rudy

Thanks for your swift and brilliantly simple reply! There are several principles in this which are new to me so it is also a useful learning opportunity for me.

One problem - it only lists the invoices with zero balances (paid) or those that are partially paid. ie if there is no payment record against an invoice then that invoice is not listed. I am rather more interested in the invoices which have a non-zero balance!

This could be my fault as I had simplified my example to demonstrate the problem and had changed some of the field names to make them more intuitive.

Here is the actual code I ended up with thanks to you, which runs with no errors:

SELECT i.invoiceid
, i.invoicedate
, ia.sum_itemamounts - p.sum_paymentamounts AS invoicebalance
FROM sa_invoices AS i
INNER
JOIN ( SELECT trinvoiceid
, SUM(tramt+(trvatrate/100*tramt)) AS sum_itemamounts
FROM sa_invoiceitems
GROUP
BY trinvoiceid ) AS ia
ON ia.trinvoiceid = i.invoiceid
INNER
JOIN ( SELECT pinvoiceid
, SUM(pamt) AS sum_paymentamounts
FROM sa_payments
GROUP
BY pinvoiceid ) AS p
ON p.pinvoiceid = i.invoiceid
ORDER
BY i.invoiceid

I should have said in my original post: For each invoice there may be several invoiceitems and several payments, or no payments.


One slight further refinement, if we can get this to list every invoice balance, will be to leave out the zero balance ones, ie the invoices which have been paid. Hopefully I can add somewhere:

WHERE invoicebalance <> 0

??

Many thanks for your help so far.

David

Last edited by bamboozled : December 11th, 2012 at 04:40 PM. Reason: Missing part

Reply With Quote
  #5  
Old December 11th, 2012, 05:43 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,353 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 4 h 17 m 19 sec
Reputation Power: 4140
Quote:
Originally Posted by bamboozled
One slight further refinement, if we can get this to list every invoice balance, will be to leave out the zero balance ones
here you go, sir --
Code:
SELECT i.invoiceid
     , i.invoicedate 
     , ia.sum_itemamounts - COALESCE(p.sum_paymentamounts,0) AS invoicebalance
  FROM sa_invoices AS i
INNER
  JOIN ( SELECT trinvoiceid 
              , SUM(tramt+(trvatrate/100*tramt)) AS sum_itemamounts
           FROM sa_invoiceitems
         GROUP
             BY trinvoiceid ) AS ia
    ON ia.trinvoiceid = i.invoiceid
LEFT OUTER    
  JOIN ( SELECT pinvoiceid
              , SUM(pamt) AS sum_paymentamounts
           FROM sa_payments
         GROUP
             BY pinvoiceid ) AS p
    ON p.pinvoiceid = i.invoiceid
 WHERE ia.sum_itemamounts <> COALESCE(p.sum_paymentamounts,0)    
ORDER 
    BY i.invoiceid

Reply With Quote
  #6  
Old December 12th, 2012, 02:56 AM
bamboozled bamboozled is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 4 bamboozled User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 15 sec
Reputation Power: 0
Case Solved!

Hi Rudy

That works perfectly! This is the first time in 20 years I've ever asked for help on a forum - I always try hard to work things out for myself, but this problem had eluded me for months.

Thanks very much!

Best wishes

David

Reply With Quote
  #7  
Old December 12th, 2012, 04:09 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Usenet aside, the earliest web-based forums date from the mid-1990s, so let's call it 18 years.

I'll get my coat...

:-)

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Joining invoices and payments - seems like a simple problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap