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
 Nested Select / vertical join/ or ....

Author  Topic 

dainova
Starting Member

31 Posts

Posted - 2009-08-03 : 14:47:45
Hi,
Do you think it's possible to construct single SELECT to get all interrelated records based on SEQ1 <--> SEQ2 relationship and having SEQ1 value, e.g.
# SEQ1 SEQ2 SOME_DATA
-.----..----...---------
1 0001 0000 ADSFASDF
2 0022 0000 ....
3 0033 0022 ....
4 0055 0033 ....
5 0067 0054 ....
6 0077 0055 ....
7 0099 0077 ....
8 0100 0031 ....

So having value SEQ1=33, I need to select recs #2,3,4,6 and 7, based on SEQ1 or SEQ2 = 33, then subsequent =22 or 33 or 55 or 77, ie.
select #1 : where seq1 or seq2 = 33 ===> returns #3,4 giving more seq=22,55
select #2 : where seq1 or seq2 = 22 or 33 or 55 ===> returns #2,3,4,6, giving extra seq=77
select #3: where seq1 or seq2 = 22 or 33 or 55 or 77 ===> returns #2,3,4,6 and 7.

it doesn't need to go beyoung 3 subslects, this shoul be enough.

Thanks
Dai

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-03 : 15:02:10
sounds like you want a "recursive CTE" although I'm not sure why your first select above returns rows 2,6,7 ???

Here is one example - search to find many more:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=127801&SearchTerms=recursive,CTE


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -