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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-06-12 : 09:51:49
|
| dan writes "Hi I an having a hard time comming up with a sql query - i think it involves recusrsion.Here is the situation:--------------------------name |Old Friend's Name--------------------------1 | 2 |13 |4 |15 |26 |3--------------------------I want a query that returns Old Friend's names of '1'.They may be his friend's or his friend's friendseg. FriendsOf(1) should return 2,4,5 5 is related to 1 via 2Thanks" |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2006-06-12 : 11:26:50
|
| So do you only want to go down two plys (friends friends) or N plys (friends friends friends...)? |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-13 : 10:58:21
|
Something like this?--datadeclare @t table (name int, OldFriendsName int)insert @t select 1, nullunion all select 2, 1union all select 3, nullunion all select 4, 1union all select 5, 2union all select 6, 3--inputdeclare @name intset @name = 1--calculationdeclare @OldFriends table (name int)insert @OldFriends select @namewhile @@rowcount > 0begininsert @OldFriends select b.name from @OldFriends a inner join @t b on a.name = b.OldFriendsName where b.name not in (select name from @OldFriends)endselect * from @OldFriends where not name = @name order by name/*resultsname ----------- 245*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|