tags:

views:

26

answers:

2

hi everybody

i need to make a select to find out X
then i need to use X to find out Y
then i need to use Y to find out Z

is there a way for making it with a single "auto-parametrized select"?

like this fanta-pseudo-code:

select Z from ( select Y from (select X from x_table) )

tnx a lot!

A: 

That's do-able with subselects, pretty much exactly as you've described there. What DB Server are you using? Can you be more specific about your queries?

MDCore
+1  A: 

Yes, absolutely. For example:

select cnt, count(*) from
( select department_id, count(*) as cnt
  from employees
  group by department_id
)
group by cnt;

This gives the "count of counts".

Or perhaps you mean something more like this, which is also valid:

select emp_name
from employees
where department_id in
( select department_id
  from departments
  where location_id in
  ( select location_id from locations
    where country = 'US'
  )
);
Tony Andrews
You can also often use a subselect as a table.
MDCore