Is it possible to save a value returned from a subquery and then use it in the same SQL statement to avoid repeating the subquery?

I am trying to do something like this:
Select t1.employee, t1.HOURS_1,
(select sum(t2.HOURS_2) from TABLE2 t2 where t2.employee = t1.employee and condition2 = CONDITION) as HOURS_2,
(t1.HOURS_1 + (select sum(t2.HOURS_2) from TABLE2 t2 where t2.employee = t1.employee and condition2 = CONDITION)) as HOURS_3
from TABLE1 t1
where condition1 = CONDITION1

I want to avoid repeating the same subquery (select sum(t2.HOURS_2) from TABLE2 t2 where t2.employee = t1.employee and condition2 = CONDITION) 
multiple times in the real  query (which is more complex than the one shown.

So I tried to use variable like this:
Declare
      Var_hours NUMBER;
Begin
Select t1.employee, t1.HOURS_1,
(select sum(t2.HOURS_2) INTO Var_hours from TABLE2 t2 where t2.employee = t1.employee and condition2 = CONDITION) as HOURS_2,
(t1.HOURS_1 + Var_hours) as HOURS_3
from TABLE1 t1
where condition1 = CONDITION1
End;

But I got Oracle error PL/SQL: ORA-01744: inappropriate INTO caused by the rule that The INTO clause may not be used in a subquery.

Is it possible to somehow write SQL statement or SQL block that would save a value returned from a subquery in order to avoid repeating the same subquery multiple times?
Thank you in advance!
pavelmedAsked:
Who is Participating?
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.

lwadwellCommented:
Hi pavelmed,

Have you tried re-writing the query to avoid sub-queries?  e.g.
Select t1.employee, t1.HOURS_1,
       sum(t2.HOURS_2) as HOURS_2,
       t1.HOURS_1 + sum(t2.HOURS_2) as HOURS_3
from TABLE1 t1
LEFT JOIN TABLE2 t2
ON t2.employee = t1.employee and condition2 = "CONDITION"
where condition1 = "CONDITION1"
GROUP BY t1.employee, t1.HOURS_1


lwadwell
0
pavelmedAuthor Commented:
Hi lwadwell,
I tried your suggestion, but got SQL errors mostly such as "not a group by expression".
I think it's because the "group by" should apply to the whole statement, while I tried tom apply it to a singe table
0
lwadwellCommented:
pavelmed,

can you show the SQL you tried?  The group by should apply to all columns being returned that are not being SUM()'d.

lwadwell
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Geert GOracle dbaCommented:
use something like this:

with X as (subquery)
select *, (select A from X where X.B = .B) from Y

 
with X as 
  (select t2.employee, sum(t2.HOURS_2) HOURS_2 from TABLE2 t2 where condition2 = 'CONDITION2' group by t2.employee)
Select t1.employee, t1.HOURS_1, X.HOURS_2,
  (t1.HOURS_1 + X.HOURS_2) as HOURS_3
from TABLE1 t1, X t2
where t1.condition1 = 'CONDITION1'
  and t1.employee = t2.employee

Open in new window

0
Geert GOracle dbaCommented:
oops typo's
with X as 
  (select t2.employee, sum(t2.HOURS_2) HOURS_2 from TABLE2 t2 where condition2 = 'CONDITION2' group by t2.employee)
Select t1.employee, t1.HOURS_1, t2.HOURS_2,
  (t1.HOURS_1 + t2.HOURS_2) as HOURS_3
from TABLE1 t1, X t2
where t1.condition1 = 'CONDITION1'
  and t1.employee = t2.employee

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
pavelmedAuthor Commented:
Geert Gruwez and lwadwell,

Thank you very much for your suggestions.
While lwadwell's suggestion was interesting, it did not fit well for my needs - I had in fact 6 different SUMs with different criteria to calculate, and to use "Group By" for each of them in one single statement would be very difficult or impossible.

But I was able to do what I needed by using the "WITH" clause - the Geert Gruwez's suggestion perfectly fit my needs, therefore I give my points to Geert Gruwez.  Thanks a lot!.

Again, thank you very much for your help.
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
Enterprise Software

From novice to tech pro — start learning today.