Link to home
Start Free TrialLog in
Avatar of pavelmed
pavelmed

asked on

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!
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

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
Avatar of pavelmed
pavelmed

ASKER

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
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
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

ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.