1

我正在尝试使用 Access 2010 创建员工组织层次结构。我的基本表包含有关员工的信息,包括他们的直接经理。理想情况下,我想创建一个包含每个级别报告的表格。此查询的净输出将是一个包含所有者、拥有者以及所有者与拥有者之间关系深度的表。

我的数据表可以总结为:

拥有的 EE ID
直接所有者 ID

我想创建的净输出是这样的表:

Owner | Owned | Levels between Owner and Owned

例如,假设 John 向 Joe 汇报,而 Joe 向 Jane 汇报。我的结构将是:

Joe  | John | 1
Jane | Joe  | 1
Jane | John | 2

我知道我的树顶(CEO)和树底(小时工)之间的深度不会超过 11 层。

在 Access SQL 中可以做这样的事情吗?理想情况下,我宁愿不编写宏,但如果必须,我会这样做。我可以自下而上或自上而下构建,我真的不在乎,但我在 Excel 中自上而下构建了我的测试文件,自上而下开始更容易,因为我会从做的 EE 开始没有直接经理(CEO)。

如果这很重要,我是 Access 的相对 n00b。谢谢你。

4

1 回答 1

1

Access SQL 不支持递归(如 SQL Server 中的递归 CTE),但以下 VBA 代码将采用名为 [Employees] 的输入表...

ID  EmployeeName    ReportsTo
--  ------------    ---------
1   Jane    
2   Joe             1
3   John            2
4   Julia           2
5   Jack            4
6   Jimbo           2
7   Jill            5

...并填充一个名为 [EmployeeHierarchy] 的表...

Superior    Subordinate LevelDifference
--------    ----------- ---------------
Jane        Joe         1
Joe         John        1
Jane        John        2
Joe         Julia       1
Jane        Julia       2
Julia       Jack        1
Joe         Jack        2
Jane        Jack        3
Jack        Jill        1
Julia       Jill        2
Joe         Jill        3
Jane        Jill        4
Joe         Jimbo       1
Jane        Jimbo       2

...像这样:

Option Compare Database
Option Explicit

Sub OrgTree()
    Dim cdb As DAO.Database, rstTop As DAO.Recordset
    Set cdb = CurrentDb
    cdb.Execute "DELETE FROM EmployeeHierarchy", dbFailOnError
    Set rstTop = cdb.OpenRecordset( _
            "SELECT ID, EmployeeName " & _
            "FROM Employees " & _
            "WHERE ReportsTo IS NULL", _
            dbOpenSnapshot)
    ' process each top-level entity
    Do While Not rstTop.EOF
        ProcessSubordinates rstTop!ID, rstTop!EmployeeName, 0
        rstTop.MoveNext
    Loop
    rstTop.Close
    Set rstTop = Nothing
    Set cdb = Nothing
End Sub

Sub ProcessSubordinates(BaseID As Long, BaseName As String, RecursionLevel As Long)
    Dim rstSubs As DAO.Recordset
    Dim i As Long, CurrentID As Long, SuperiorID As Long, SuperiorName As String
    Set rstSubs = CurrentDb.OpenRecordset( _
            "SELECT ID, EmployeeName " & _
            "FROM Employees " & _
            "WHERE ReportsTo = " & BaseID, _
            dbOpenSnapshot)
    ' loop through immediate subordinates for the "base" employee
    Do While Not rstSubs.EOF
        AddToEmployeeHierarchy BaseName, rstSubs!EmployeeName, 1

        ' traverse back up the tree to list all other superiors
        CurrentID = BaseID
        For i = 1 To RecursionLevel
            SuperiorID = DLookup("ReportsTo", "Employees", "ID = " & CurrentID)
            SuperiorName = DLookup("EmployeeName", "Employees", "ID = " & SuperiorID)
            AddToEmployeeHierarchy SuperiorName, rstSubs!EmployeeName, i + 1
            CurrentID = SuperiorID
        Next

        ' and recurse to process down the tree for the current subordinate
        ProcessSubordinates rstSubs!ID, rstSubs!EmployeeName, RecursionLevel + 1
        rstSubs.MoveNext
    Loop
    rstSubs.Close
    Set rstSubs = Nothing
End Sub

Sub AddToEmployeeHierarchy(SuperiorName As String, SubordinateName As String, LevelDifference As Long)
    Dim rst As DAO.Recordset
    Set rst = CurrentDb.OpenRecordset("EmployeeHierarchy", dbOpenTable)
    rst.AddNew
    rst!Superior = SuperiorName
    rst!Subordinate = SubordinateName
    rst!LevelDifference = LevelDifference
    rst.Update
    rst.Close
    Set rst = Nothing
End Sub
于 2013-08-29T15:38:24.453 回答