Solved

SQL Query, difference between two sums

Posted on 2004-09-02
7
474 Views
Last Modified: 2012-05-05
I would like to write a query that calculates the difference between two sums created by seperate select statements.  For example, here are my two selects:

SELECT  SUM(Loans.Amount) AS Amount
FROM Persons
INNER JOIN Loans
      ON Loans.PersonsID = Persons.PersonsID
INNER JOIN Terms
      ON Terms.TermsID = Loans.TermsID
WHERE Terms.Name = 'Fall 2004'
GROUP BY Loans.PersonsID


SELECT SUM(Transactions.CurrentAmount) AS BalanceAmount
FROM Transactions
INNER JOIN Terms
      ON Transactions.TermsID = Terms.TermsID
WHERE (Terms.Name = 'Fall 2004')
GROUP BY Transactions.EntityID


Basically, what I'd like to do is return RemainingAmount = (BalanceAmount - Amount), using one statement including those above.
0
Comment
Question by:jroof
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:AustinSeven
ID: 11963607

declare @sumA int, @sumB int -- or use decimal data type
select @sumA = select ...  -- first sum query

select @sumB = select ...  -- other sum query

select @sumA  + @sumB

AustinSeven
0
 

Author Comment

by:jroof
ID: 11963700
Thanks, if possible, I need to do this withough variables.  Thanks.
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 125 total points
ID: 11963743
Select BalanceAmount - Amount as RemainingAmount  
from (
SELECT  SUM(Loans.Amount) AS Amount
FROM Persons
INNER JOIN Loans
     ON Loans.PersonsID = Persons.PersonsID
INNER JOIN Terms
     ON Terms.TermsID = Loans.TermsID
WHERE Terms.Name = 'Fall 2004'
GROUP BY Loans.PersonsID
) a cross join (
SELECT SUM(Transactions.CurrentAmount) AS BalanceAmount
FROM Transactions
INNER JOIN Terms
     ON Transactions.TermsID = Terms.TermsID
WHERE (Terms.Name = 'Fall 2004')
GROUP BY Transactions.EntityID
) b
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 26

Expert Comment

by:Hilaire
ID: 11963795
I had not noticed the GROUP BY 's
My method above won't work unless you filter to get one single line (group) per query, or if you remove the GROUP BY clauses.

How are the two queries related ?

0
 

Author Comment

by:jroof
ID: 11964106
They're not related per say.  Here's the purpose...A student gets a number of loans over the school year.  The first select is calculating his total loans.  A student puts debits against his total loan monies.  This is the second query.  The purpose of t his query is to find the running balance the student has in remaining funds.
0
 

Author Comment

by:jroof
ID: 11964141
By the way, this query will only be used for one student at a time. I will also be passing a studentID using a seperate macro so will the cross join work in that case?
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 11964167
Yep, as long as both queries return one single row
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

706 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

20 Experts available now in Live!

Get 1:1 Help Now