Subtracting from subqueries

Is there a way for me to do math on 2 subqueries? IE. I have 2 columns that pull data and I want to then have a third column value equal col1-col2. I can run the subqueries all over again, but I think there is a better way.
centricCAsked:
Who is Participating?
 
cyberkiwiConnect With a Mentor Commented:
You can make a subquery of the original, and refer to both columns, e.g.

select col1, col2, (select <subquery1>) sum1, (select <subquery2>) sum2
from sometable

Open in new window


becomes

select col1, col2, sum1, sum2, sum1-sum2 as NewColumn
FROM
(
select col1, col2, (select <subquery1>) sum1, (select <subquery2>) sum2
from sometable
) X

Open in new window


Make sure to alias the subquery, as required by syntax
0
 
Ephraim WangoyaCommented:


select a, b, c-d as e
from
  (select a, b, (select x from t1) c, (select y from t2) d
   from aa
  )

 
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.