SQL分层查询最多10级

时间:2017-05-22 11:41:10

标签: sql-server hierarchical-query

我想编写一个SQL Server SELECT查询,以表格格式显示分层数据。

喜欢:员工向X经理报告,然后X经理向XX经理报告,然后XX经理向XXX经理报告 - 每行记录。

表格数据:

Input data format

输出格式:

output required format

1 个答案:

答案 0 :(得分:1)

简单,一起加入10个虚拟表

Select person, Person1, Person2, ...
from people
left join
    (
    select person as Person1, Manager as Manager2 from People
    ) as People1
        on people.Manager = Person1
left join
    (
    select person as Person2, Manager as Manager3 from People
    ) as People2
        on Manager2 = Person2
....