Avatar of Badotz
Badotz
Flag for United States of America asked on

How to pass a param to a query from within another query

Friends,

Is it possible to pass a param to a query from within another query?

Query1:
select Sum(hours) as total_hours from tot_hours where job_number = [@Job_Number] group by job_number;

Query2:
select j.job_number, j.job_name, q.total_hours from job_master as j, Query1 as q;

How would I pass "job_number" to Query1 from within Query2?

Or am I completely off the mark?
SQL

Avatar of undefined
Last Comment
Badotz

8/22/2022 - Mon
Aneesh

select Sum(hours) as total_hours from tot_hours where job_number IN ( SELECT JobNumberFROM (select j.job_number, j.job_name, q.total_hours from job_master as j, Query1 as q)    )
group by job_number
Badotz

ASKER
I'll give that a try, thanks.
Badotz

ASKER
After further review, your example does not make sense.

The first part "select Sum(hours) as total_hours from tot_hours where job_number" is in Query1, yet Query1 is in the subquery, referenced as "q" - which, by the way, is never referenced.

Am I missing something here?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Aneesh

Something like this

;

Query2:
select j.job_number, j.job_name, q.total_hours from job_master as j,

(select  job_number,Sum(hours) as total_hours from tot_hours where job_number = [@Job_Number] group by job_number )   as q

WHERE  j.job_number = qj.job_number
ASKER CERTIFIED SOLUTION
ee_rlee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Badotz

ASKER
Yes, but [@Job_Number] in Query1 is to be replaced by the value of the "job_number" column from Query2. How does the above accomplish that?

And I assume that "qj.job_number" s.b. "q.job_number"?
SOLUTION
Aneesh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Badotz

ASKER
Many thanks for the help.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.