Paul,
In SQL Server 2005, finding this information is not
as hard as it used to be. Here's an example from
the AdventureWorks sample database that calculates
the total number of subordinate employees for each
manager. By default, recursive queries allow 100
levels of recursion before generating an error, though
this can be changed with the MAXRECURSION
option, if your hierarchy can be deeper.
with Ancestry(ManagerID,Subordinate) as (
select
ManagerID, EmployeeID
from AdventureWorks.HumanResources.Employee
union all
select
A.ManagerID,
E.EmployeeID
from AdventureWorks.HumanResources.Employee as E
join Ancestry as A
on A.Subordinate = E.ManagerID
)
select ManagerID, count(*) as subs
from Ancestry
group by ManagerID
-- Steve Kass
-- Drew University
[quoted text, click to view] Paul wrote:
>Thanks.
>
>Actually, at this point it is only 2 or three deep, but you are correct, I
>am interested in an arbitrarily deep nesting.
>...<snip>...
>
>
>>If this is a hierarchy of unknwn depth, you will need a different
>>model. Google up Nested Sets for trees.
>>
>>
>>
>
>
>