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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Recursive Query

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2000-10-16 : 18:39:06
Suraj writes "<Table border=0 cellspacing=0 cellpadding=0>
<tr> <td>I need your help regarding this tough SQL query my colleague has asked me to solve.</td></tr><tr> <td>Table: Tempo
</td></tr>
<tr> <td>
Fields: AccessID(int), DependentID(int)
</td></tr>
<tr> <td>
Tempo has the following rows.
</td></tr>
<tr> <td>
<table border =1>
<tr>
<td> AccessID </td>
<td> DependentID </td>
</tr>
<tr>
<td> 1 </td>
<td> 2 </td>
</tr>
<tr>
<td> 2 </td>
<td> 3 </td>
</tr>
<tr>
<td> 3 </td>
<td> 4 </td>
</tr>
<tr>
<td> 4 </td>
<td> 5 </td>
</tr>
<tr>
<td> 5 </td>
<td> 6 </td>
</tr>
<tr>
<td> 6 </td>
<td> 7 </td>
</tr>

</table>
</tr> </td>

<tr> <td>
The query is to return me records for a given Accessid going all
</tr> </td>
<tr> <td>
the way to the root. For example: for AccessID = 4, the records
</tr> </td>
<tr> <td>
returned should be:
</tr> </td>

<tr> <td>
<table border=1>
<tr>
<td> AccessID </td>
<td> DependentID </td>
</tr>
<tr>
<td> 4 </td>
<td> 5 </td>
</tr>
<tr>
<td> 3 </td>
<td> 4 </td>
</tr>
<tr>
<td> 2 </td>
<td> 3 </td>
</tr>
<tr>
<td> 1 </td>
<td> 2 </td>
</tr>
</table>
</tr> </td>

<tr> <td>
since, '4' is a dependent on '3', and '3' is dependent on '2',
</tr> </td>
<tr> <td>
and '2' is depdendent on '1' ... or in other words
</tr> </td>
<tr> <td>
1 --> 2 --> 3 --> 4 etc. So for Access ID = 4, the path should </tr> </td>
<tr> <td>
be retraced 4 --> 3 --> 2 --> 1.
</tr> </td>
<tr> <td>
We are using SQL Server 6.5 and would be upgrading to 7, running
</tr> </td>
<tr> <td>
on Windows NT workstation (Service pack 6a).
</tr> </td>

<tr> <td>
Thanks
</tr> </td>
</table>"
   

- Advertisement -