Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combine Three Queries?

Author  Topic 

planetoneautomation
Posting Yak Master

105 Posts

Posted - 2010-07-28 : 15:24:43
New to this ... think this is a JOIN issue but there are three queries (tables) involved and I'm a little confused.

This query returns a list of id's ...

select tc_cycle_id from testcycl

... I loop through the id's from the above query and execute the following query for each of those id's ...

select cy_folder_id from cycle where cy_cycle_id = <id_from_prev_query>

...I loop through the id's from the above query and execute the following query for each of those id's ...

select cf_item_name from cycl_fold where cf_item_id = <id_from_prev_query>

As you can see, a large number of rows returned by the first and/or second queries could yield tons of iterations. Isn't there a way to construct a single query using two JOINS?

Any help is appreciated.

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-07-28 : 15:31:30
Maybe this..
select a.tc_cycle_id 
,b.cy_folder_id
,c.cf_item_name
from testcycl a
inner join cycle b on b.cy_cycle_id = a.tc_cycle_id
inner join cycl_fold c on c.cf_item_id = b.cy_folder_id
Go to Top of Page
   

- Advertisement -