Solved

SQL: Join two SELECTs to calculate difference

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
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…
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

867 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

15 Experts available now in Live!

Get 1:1 Help Now