[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# SQL Query, difference between two sums

Posted on 2004-09-02
Medium Priority
525 Views
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
Question by:jroof
• 3
• 3

LVL 10

Expert Comment

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

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

LVL 26

Accepted Solution

Hilaire earned 500 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

LVL 26

Expert Comment

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

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

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

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

## Featured Post

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then readingâ€¦
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
###### Suggested Courses
Course of the Month11 days, 6 hours left to enroll