Solved

SQL: Join two SELECTs to calculate difference

Posted on 2004-09-10
5
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

752 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