Solved

SQL: Join two SELECTs to calculate difference

Posted on 2004-09-10
5
616 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 26
sql server concatenate fields 10 30
Simple SQL query from two tables 13 51
How to search for strings inside db views 4 24
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…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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

816 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

10 Experts available now in Live!

Get 1:1 Help Now