Solved

SQL: Join two SELECTs to calculate difference

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

747 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

13 Experts available now in Live!

Get 1:1 Help Now