Home Forums SQL Server 2008 T-SQL (SS2K8) Hierarchy example - Ken Henderson's book - not working RE: Hierarchy example - Ken Henderson's book - not working

  • Like this? (All the same queries, but modified with Null. This leaves Groucho out altogether).

    create table staff(employee int primary key, employee_name varchar(10),

    supervisor int null references staff (employee))

    insert staff

    values

    (1, 'groucho', Null),

    (2, 'chico', 1),

    (3, 'harpo', 2),

    (4, 'zeppo', 2),

    (5, 'moe', 1),

    (6, 'larry', 5),

    (7, 'curly', 5),

    (8, 'shemp', 5),

    (9, 'joe', 8),

    (10, 'curly joe', 9);

    select chartdepth=1, employee=o2.employee, supervisor=o1.employee

    into #org_chart

    from staff o1 inner join staff o2 on (o1.employee=o2.supervisor)

    where o1.supervisor is not null

    insert into #org_chart

    select distinct o1.chartdepth+1, o2.employee, o1.supervisor

    from #org_chart o1 join #org_chart o2 on (o1.employee=o2.supervisor)

    where o1.chartdepth=(select max(chartdepth) from #org_chart)

    and o1.supervisor is not null

    select s.employee_name, supervises='supervises', e.employee_name

    from #org_chart o join staff s on (o.supervisor=s.employee)

    inner join staff e on (o.employee=e.employee)

    where o.supervisor<>o.employee

    --Quote me