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?
 
Guy Hengel [angelIII / a3]Connect With a Mentor 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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.