【问题标题】:SQL Query to find all the superiors of a particular subordinateSQL 查询查找特定下属的所有上级
【发布时间】:2012-01-21 02:15:39
【问题描述】:

我的表格包含 3 列

Emp_ID |  Emp_Name   |  Emp_Manager_ID
========================================
1      |  Admin      |   Null         
2      |  John       |   1            
3      |  Sam        |   2             
4      |  Mike       |   2            
5      |  Jeff       |   4            
6      |  Ben        |   3            
7      |  Vicky      |   5

参数 id @Emp_ID = 7 期望的结果是查找给定 Emp_Id 下的所有上级所以结果应该是所有 EmpIDs 6,5,4,3,2,1 因为 5 是 7 的经理,4 是5 和 2 的经理是 4,3 的经理,3 是 6 的经理,1 是 2 的经理。

【问题讨论】:

    标签: sql sql-server-2005 recursion common-table-expression


    【解决方案1】:

    这在树上起作用,在你的情况下给出 7 -> 5 -> 4 -> 2 -> 1 然后停止。

    WITH
      unrolled_branch AS
    (
      SELECT
        emp_id,
        emp_name,
        emp_mnager_id
      FROM
        yourTable
      WHERE
        emp_id = @emp_id
    
      UNION ALL
    
      SELECT
        your_table.emp_id,
        your_table.emp_name,
        your_table.emp_mnager_id
      FROM
        yourTable
      INNER JOIN
        unrolled_branch
          ON unrolled_branch.emp_manager_id = yourTable.emp_id
    )
    SELECT
      *
    FROM
      unrolled_branch
    


    我不知道你是如何获得 3 和 6 的。你的意思是说他们和其他人是一个级别的吗?

    你有这棵树...

    1-2-3-6
       \
        4-5-7
    

    但是这棵树的结果应该是什么?

          A-B-C
         /
    1-2-3-6
       \
        4-5-7
    

    您是否强制执行任何一个节点可能只有一个父节点,并且任何树中的一个节点必须没有父节点的约束?或者这些都可以吗?

        3   9     5-6     7       C     C
       / \ /         \   /       / \   / \
    1-2   6-8         3-4       A   D-A   D- (etc, etc)
       \ /           /   \       \ /   \ /
        4-5-7     1-2     8       B     B
    

    【讨论】:

    • 感谢回复,但没有显示6
    • 3 是怎么来的,因为 2 是 4 的上级,3 它也应该是 6,因为 3 是 6 的上级
    • @MathewPaul - 正如我的回答所说,我理解为什么我的代码不显示 3 和 6。所以我会询问其他场景,以便在分支返回时更准确地阐述您的业务规则树。目前,您的描述似乎要求“返回树中的每个节点”(爬上每个分支,然后也返回每个分支 => 到达每个节点?还是有一些结束条件? )
    • 感谢您的解释我认为我的部分有误
    • 是的,预期结果是 5,4,2,1
    【解决方案2】:

    您需要设置一个递归 CTE 来遍历您的层次结构树。看看,例如,http://www.4guysfromrolla.com/webtech/071906-1.shtml

    【讨论】:

      【解决方案3】:

      这里是完整的例子:

      create table #employees (
      Emp_ID int,
      Emp_Name varchar(10),
      Emp_Manager_ID int
      )
      
      
      create table #superiors (
      Emp_ID int
      )
      
      insert into #employees values (1, 'Admin', NULL)
      insert into #employees values (2, 'John', 1)
      insert into #employees values (3, 'Sam', 2)
      insert into #employees values (4, 'Mike', 2)
      insert into #employees values (5, 'Jeff', 4)
      insert into #employees values (6, 'Ben', 3)
      insert into #employees values (7, 'Vicky', 5)
      
      declare @count int, @testID int
      select @testID = 7
      
      select @count = NULL
      select @count = Emp_Manager_ID from #employees where Emp_ID = @testID
      
      while(@count IS NOT NULL)
      begin
        insert into #superiors values (@count)
        select @count = Emp_Manager_ID from #employees where Emp_ID = @count
      end
      
      
      
      select * from #superiors
      
      
      drop table #employees
      drop table #superiors 
      

      【讨论】:

        猜你喜欢
        • 2012-01-11
        • 1970-01-01
        • 2017-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        • 1970-01-01
        相关资源
        最近更新 更多