Solved

SQL: Join two SELECTs to calculate difference

Posted on 2004-09-10
5
618 Views
Last Modified: 2007-12-19
This is similar to a question I've already asked but with a new wrinkle.
 have the following query:
SELECT
CMB_AccountsID, CurrentAmount - Amount AS BalanceAmount
FROM

(SELECT SUM(Fund.Amount) AS Amount
FROM Persons
INNER JOIN Fund
ON Fund.PersonsID= Persons.PersonsID
INNER JOIN Terms
ON Terms.TermsID = Fund.TermsID
INNER JOIN Accounts
ON Accounts.PersonsID = Persons.PersonsID
WHERE Terms.Name = 'Fall 2004'
AND Accounts.AccountsID =
(SELECT DISTINCT AccountsID
FROM Transactions
WHERE Transactions.AccountsID IN (1, 3))
GROUP BY Accounts.AccountsID) A

CROSS JOIN

(SELECT SUM(Transactions.CurrentAmount) AS CurrentAmount,
Account.AccountsID
FROM Transactions
INNER JOIN Terms
ON Transactions.TermsID = Terms.TermsID
WHERE (Terms.Name = 'Fall 2004')
AND Transactions.AccountsID IN (1,3)
GROUP BY Transactions.AccountsID) B



THis query works fine if the AccountID is limited to one row, by nature of the CROSS JOIN. But when I return multiple values in the IN clause, the query will not work. I'm looking to accomplish the logic above but by another method. Any ideas would be greatly appreciated.
0
Comment
Question by:jroof
  • 2
5 Comments
 
LVL 7

Expert Comment

by:ChrisFretwell
ID: 12030134
Do you need to cross join or do you want to join on accountsid? Also, not sure you need to do all you're doing in here _ this should fix first problem, may look at your query for more after....

SELECT
CMB_AccountsID, isnull(CurrentAmount,0) - Amount AS BalanceAmount
FROM

(SELECT SUM(Fund.Amount) AS Amount
FROM Persons
INNER JOIN Fund
ON Fund.PersonsID= Persons.PersonsID
INNER JOIN Terms
ON Terms.TermsID = Fund.TermsID
INNER JOIN Accounts
ON Accounts.PersonsID = Persons.PersonsID
WHERE Terms.Name = 'Fall 2004'
AND Accounts.AccountsID =
(SELECT DISTINCT AccountsID
FROM Transactions
WHERE Transactions.AccountsID IN (1, 3))
GROUP BY Accounts.AccountsID) A

Left Join

(SELECT transactions.accountsid, SUM(Transactions.CurrentAmount) AS CurrentAmount,
Account.AccountsID
FROM Transactions
INNER JOIN Terms
ON Transactions.TermsID = Terms.TermsID
WHERE (Terms.Name = 'Fall 2004')
AND Transactions.AccountsID IN (1,3)
GROUP BY Transactions.AccountsID) B on A.accountis = b.accountsid
0
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 63 total points
ID: 12030173
I also suspect you could simplify, but here's something else for you to try:

SELECT CMB_AccountsID, CurrentAmount - Amount AS BalanceAmount
FROM
(SELECT SUM(Fund.Amount) AS Amount
FROM Persons
INNER JOIN Fund ON Fund.PersonsID= Persons.PersonsID
INNER JOIN Terms ON Terms.TermsID = Fund.TermsID
INNER JOIN Accounts ON Accounts.PersonsID = Persons.PersonsID
WHERE Terms.Name = 'Fall 2004'
AND EXISTS (SELECT AccountsID
      FROM Transactions
      WHERE Transactions.AccountsID IN (1, 3)
      AND Transactions.AccountsID = Accounts.AccountsID ) A
CROSS JOIN
(SELECT SUM(Transactions.CurrentAmount) AS CurrentAmount, Account.AccountsID
FROM Transactions
INNER JOIN Terms ON Transactions.TermsID = Terms.TermsID
WHERE (Terms.Name = 'Fall 2004') AND Transactions.AccountsID IN (1,3)
GROUP BY Transactions.AccountsID ) B
0
 
LVL 7

Assisted Solution

by:ChrisFretwell
ChrisFretwell earned 62 total points
ID: 12030211
I was just looking at that jd. I think its being much more complicated than it needs to be.

Not sure why there is a cross join at all - and think even an inner is fine since both the upper and lower have the same criteria and there must be at least 1 transaction.

There are only 2 account ids returned (1,3)

I think this might work.....
 Comment from ChrisFretwell
Date: 09/10/2004 12:34PM PDT
 Your Comment  


Do you need to cross join or do you want to join on accountsid? Also, not sure you need to do all you're doing in here _ this should fix first problem, may look at your query for more after....

SELECT
CMB_AccountsID, isnull(CurrentAmount,0) - Amount AS BalanceAmount
FROM

(SELECT SUM(Fund.Amount) AS Amount
FROM Persons
INNER JOIN Fund
ON Fund.PersonsID= Persons.PersonsID
INNER JOIN Terms
ON Terms.TermsID = Fund.TermsID
INNER JOIN Accounts
ON Accounts.PersonsID = Persons.PersonsID
WHERE Terms.Name = 'Fall 2004'
AND Accounts.AccountsID =
(SELECT DISTINCT AccountsID
FROM Transactions
WHERE Transactions.AccountsID IN (1, 3))
GROUP BY Accounts.AccountsID) A

Left Join

(SELECT transactions.accountsid, SUM(Transactions.CurrentAmount) AS CurrentAmount,
Account.AccountsID
FROM Transactions
INNER JOIN Terms
ON Transactions.TermsID = Terms.TermsID
WHERE (Terms.Name = 'Fall 2004')
AND Transactions.AccountsID IN (1,3)
GROUP BY Transactions.AccountsID) B on A.accountis = b.accountsid

 
Comment from jdlambert1
Date: 09/10/2004 12:38PM PDT
 Comment  


I also suspect you could simplify, but here's something else for you to try:
SELECT AccountsID, CurrentAmount - Amount AS BalanceAmount
FROM
(SELECT Terms.accountsid, CurrentAmount, SUM(Fund.Amount) AS Amount
FROM Persons
INNER JOIN Fund ON Fund.PersonsID= Persons.PersonsID
INNER JOIN Terms ON Terms.TermsID = Fund.TermsID
INNER JOIN Accounts ON Accounts.PersonsID = Persons.PersonsID
inner join
(SELECT transactions.accountsidSUM(Transactions.CurrentAmount) AS CurrentAmount, Account.AccountsID
FROM Transactions
INNER JOIN Terms ON Transactions.TermsID = Terms.TermsID
WHERE (Terms.Name = 'Fall 2004') AND Transactions.AccountsID IN (1,3)
GROUP BY Transactions.AccountsID ) B on Terms.accountsid = b.accountsid
WHERE Terms.Name = 'Fall 2004'
group by Terms.accountsid, CurrentAmount
) A
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

820 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