Solved

VB.net SQL Combine two SQL Statements into one

Posted on 2014-09-15
4
234 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

773 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