Solved

VB.net SQL Combine two SQL Statements into one

Posted on 2014-09-15
4
232 Views
Last Modified: 2014-09-16
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
0
Comment
Question by:murbro
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40323668
select
CustomerID, sum(amount) amount
from
 (
   select [Link ID] CustomerID, Amount from payments
   union all
   select customerId, [inv amt] from invoices
) SubQuery
group by CustomerID
0
 

Author Closing Comment

by:murbro
ID: 40323692
Great answer! Thanks very much
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40324640
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.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40324757
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
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now