Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net SQL Combine two SQL Statements into one

Hi
I use the following two SQL Statements to get total amount invoiced to customers
and total amount paid by customers. results look like the table in the image.
How do I get the total balance (Amount Invoiced less Amount Paid) using one SQL
statement. What SQL statement would I use

Select [LINK ID] As CustomerID,SUM([AMOUNT]) As AMOUNT From Payments GROUP BY [LINK ID]
Select CustomerID, SUM([Inv Amt]) As AMOUNT From Invoices Group By CustomerID
Image1.jpg
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Great answer! Thanks very much
Just curious, but don't you deduct payments from invoices?

If I invoiced you $1,000 and you paid me $1,000, the outstanding amount is $0; NOT $2,000

Are you sure you just want to add all those values? Seems like a very curious figure that may have little or no meaning
 - it may even be misleading.
I have subsequently noticed you had a deleted question regarding "VB.net SQL Statement to show balance owed" this makes me even more concerned about the validity of just summing these numbers. Maybe this would help?

CustomerID, sum(amount) amount
from
 (
   select [Link ID] CustomerID, -Amount from payments --<< change this line to make the Amounts values negative
   union all
   select customerId, [inv amt] from invoices
) SubQuery
group by CustomerID