Solved

VB.net SQL Combine two SQL Statements into one

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

744 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

13 Experts available now in Live!

Get 1:1 Help Now