开发者

AUTO-Parametrized multiple SELECT

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!


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'
  )
);


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?


Here is an in depth example trying to to find all "apartment communities" in the same county as another property that is provided.

select ZipCode.countyname AS "County Name" from RMPROP INNER JOIN ZipCode ON RMPROP.ZIP = ZIPCODE.ZIPCODE WHERE INACTIVE = 'N' AND ZIP IN (select zipcode from zipcode where (COUNTYNAME + STATEABBR) IN(select countyname+stateabbr from zipcode where zipcode =(SELECT Zip FROM RMPROP WHERE RMPROPID = '113')))

Think about it as building a select statement for the first set of data you need to find, put parenthesis around that statement, and then query against it has if it were a table in a FROM clause.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜