Nested Select Statements w/ addition

Basically, I want to know if there is a way to run 2 nested select statements in a query and then take the value of those and run a calculation on them without having to re-query them (or duplicate the selects in the statement).  Attached is a simplified example of what I'd like to do, but doesn't work (my real statement is very large which is why I'm looking for a better way to do this).  Obviously, it fails on the line that adds the 2 values together because it can't use those even though I've named them.  I get that, but I'm wondering if there's a good way around it without writing a custom stored procedure or function.
Select a.ACCOUNT_NUMBER,
	(Select Sum(INCOME_CASH_AMOUNT) From TRANSACTIONS Where TRANSACTIONS.ACCOUNT_NUMBER=t.ACCOUNT_NUMBER) As INCOME_AMOUNT,
	(Select Sum(PRINCIPAL_CASH_AMOUNT) From TRANSACTIONS Where TRANSACTIONS.ACCOUNT_NUMBER=t.ACCOUNT_NUMBER) As PRINCIPAL_AMOUNT,
	(Select INCOME_AMOUNT + PRINCIPAL_AMOUNT) As TOTAL_AMOUNT
From ACCOUNT a
Inner Join TRANSACTIONS t On t.ACCOUNT_NUMBER = a.ACCOUNT_NUMBER

Open in new window

LVL 3
IUFITSAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
what about this:
Select a.ACCOUNT_NUMBER
, Sum(t.INCOME_CASH_AMOUNT) As INCOME_AMOUNT
, Sum(t.PRINCIPAL_CASH_AMOUNT) As PRINCIPAL_AMOUNT
, Sum(t.INCOME_CASH_AMOUNT + t.PRINCIPAL_CASH_AMOUNT)As TOTAL_AMOUNT
From ACCOUNT a
Inner Join TRANSACTIONS t On t.ACCOUNT_NUMBER = a.ACCOUNT_NUMBER
group by a.ACCOUNT_NUMBER

Open in new window

0
IUFITSAuthor Commented:
That would work well in the simplified example I provided.  
My sub-queries are a lot larger with joins and on each that do a lot of calculating, and they're bulky text wise which makes the query difficult to read. I guess what I'm asking is, is there a way to re-use the INCOME_AMOUNT value that I've selected in another calculation (or, even in a where statement for that matter).
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
no, you cannot immediately reuse a column name.

you can do this:
select ACCOUNT_NUMBER
  , INCOME_AMOUNT
  , PRINCIPAL_AMOUNT
  , INCOME_CASH_AMOUNT + PRINCIPAL_CASH_AMOUNT As TOTAL_AMOUNT
FROM (
Select a.ACCOUNT_NUMBER
, Sum(t.INCOME_CASH_AMOUNT) As INCOME_AMOUNT
, Sum(t.PRINCIPAL_CASH_AMOUNT) As PRINCIPAL_AMOUNT
From ACCOUNT a
Inner Join TRANSACTIONS t On t.ACCOUNT_NUMBER = a.ACCOUNT_NUMBER
group by a.ACCOUNT_NUMBER
) as subquery

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

IUFITSAuthor Commented:
Ahhhhhah!   That answers my question and gives me a good route to go on.  I'm going to accept that answer but I have one more question in that is there anything wrong with running the query like that that you can think of off the top of your head?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
nothing wrong AFAIK, especially with "complex" subqueries it will save lots of code and CPU/IO ...
0
IUFITSAuthor Commented:
Thanks again angellll, it's much appreciated!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.