7

I have the following tables:

Employees
-------------
ClockNo     int
CostCentre  varchar
Department  int

and

Departments
-------------
DepartmentCode  int
CostCentreCode  varchar
Parent          int

Departments can have other departments as parents meaning there is infinite hierarchy. All departments belong to a cost centre and so will always have a CostCentreCode. If parent = 0 it is a top level department

Employees must have a CostCentre value but may have a Department of 0 meaning they are not in a department

What I want to try and generate is a query that will give the up to four levels of hierarchy. Like this:

EmployeesLevels
-----------------
ClockNo
CostCentre
DeptLevel1
DeptLevel2
DeptLevel3
DeptLevel4

I've managed to get something to display the department structure on it's own, but I can't work out how to link this to the employees without creating duplicate employee rows:

SELECT d1.Description AS lev1, d2.Description as lev2, d3.Description as lev3, d4.Description as lev4
FROM departments AS d1
LEFT JOIN departments AS d2 ON d2.parent = d1.departmentcode
LEFT JOIN departments AS d3 ON d3.parent = d2.departmentcode
LEFT JOIN departments AS d4 ON d4.parent = d3.departmentcode
WHERE d1.parent=0;

SQL To create Structure and some sample data:

CREATE TABLE Employees(
ClockNo integer NOT NULL PRIMARY KEY,
CostCentre varchar(20) NOT NULL,
Department integer NOT NULL);

CREATE TABLE Departments(
DepartmentCode integer NOT NULL PRIMARY KEY,
CostCentreCode varchar(20) NOT NULL,
Parent integer NOT NULL
);

CREATE INDEX idx0 ON Employees (ClockNo);
CREATE INDEX idx1 ON Employees (CostCentre, ClockNo);
CREATE INDEX idx2 ON Employees (CostCentre);

CREATE INDEX idx0 ON Departments (DepartmentCode);
CREATE INDEX idx1 ON Departments (CostCentreCode, DepartmentCode);

INSERT INTO Employees VALUES (1, 'AAA', 0);
INSERT INTO Employees VALUES (2, 'AAA', 3);
INSERT INTO Employees VALUES (3, 'BBB', 0);
INSERT INTO Employees VALUES (4, 'BBB', 4);
INSERT INTO Employees VALUES (5, 'CCC', 0); 
INSERT INTO Employees VALUES (6, 'AAA', 1);
INSERT INTO Employees VALUES (7, 'AAA', 5);
INSERT INTO Employees VALUES (8, 'AAA', 15);

INSERT INTO Departments VALUES (1, 'AAA', 0);
INSERT INTO Departments VALUES (2, 'AAA', 1);
INSERT INTO Departments VALUES (3, 'AAA', 1);
INSERT INTO Departments VALUES (4, 'BBB', 0);
INSERT INTO Departments VALUES (5, 'AAA', 3);
INSERT INTO Departments VALUES (12, 'AAA', 5);
INSERT INTO Departments VALUES (15, 'AAA', 12);

This gives the following structure (employee clock numbers in square brackets):

Root
  |
  |---AAA                   [1]
  |    \---1                [6]
  |       |---2     
  |       \---3             [2]
  |          \---5          [7]
  |             \---12
  |                \---15   [8]
  |
  |---BBB                   [3]
  |    \---4                [4]
  |
  \---CCC                   [5]

The query should return the following:

ClockNo CostCentre Level1 Level2 Level3 Level4
1       AAA        
2       AAA        1      3
3       BBB
4       BBB        4
5       CCC
6       AAA        1
7       AAA        1      3       5
8       AAA        1      3       5      12  *

* In the case of Employee 8, they are in level5. Ideally I would like to show all their levels down to level4, but I am happy just to show the CostCentre in this case

4

7 回答 7

4

当我们加入表时,当我们找到属于上一级员工的适当部门时,我们应该停止进一步遍历路径。

当Employee.Department = 0时,我们也有例外情况。在这种情况下,我们不应该加入任何部门,因为在这种情况下,部门是根。

我们只需要在其中一个级别选择包含员工部门的那些记录。如果员工的部门级别大于 4,我们应该展开所有 4 级别的部门并按原样显示(即使无法达到所需的部门级别并且在展开的部门中找不到它)。

select e.ClockNo, 
       e.CostCentre, 
       d1.DepartmentCode as Level1, 
       d2.DepartmentCode as Level2, 
       d3.DepartmentCode as Level3, 
       d4.DepartmentCode as Level4
from Employees e
left join Departments d1 
          on e.CostCentre=d1.CostCentreCode 
          and d1.Parent=0 
          and ((d1.DepartmentCode = 0 and e.Department = 0) or e.Department <> 0)
left join Departments d2 
          on d2.parent=d1.DepartmentCode 
          and (d1.DepartMentCode != e.Department and e.Department<>0)
left join Departments d3 
          on d3.parent=d2.DepartmentCode 
          and (d2.DepartMentCode != e.Department and e.Department<>0)
left join Departments d4 
          on d4.parent=d3.DepartmentCode 
          and (d3.DepartMentCode != e.Department and e.Department<>0)
where e.Department=d1.DepartmentCode 
      or e.Department=d2.DepartmentCode 
      or e.Department=d3.DepartmentCode 
      or e.Department=d4.DepartmentCode 
      or e.Department=0
      or (
        (d1.DepartmentCode is not null) and
        (d2.DepartmentCode is not null) and
        (d3.DepartmentCode is not null) and
        (d4.DepartmentCode is not null)
      )
order by e.ClockNo;
于 2016-04-18T11:17:52.273 回答
2
SELECT  [ClockNo]
    ,   [CostCentre]    
    ,   CASE
            WHEN Department <> 0 THEN dept.[Level1]         
        END AS [Level1]
    ,   CASE
            WHEN Department <> 0 THEN dept.[Level2]         
        END AS [Level2]
    ,   CASE
            WHEN Department <> 0 THEN dept.[Level3]         
        END AS [Level3]
    ,   CASE
            WHEN Department <> 0 THEN dept.[Level4]         
        END AS [Level4]

FROM    [Employees] emp
LEFT JOIN
(
SELECT  
        CASE 
            WHEN d4.[DepartmentCode] IS NOT NULL THEN d4.[DepartmentCode]
            WHEN d3.[DepartmentCode] IS NOT NULL THEN d3.[DepartmentCode]
            WHEN d2.[DepartmentCode] IS NOT NULL THEN d2.[DepartmentCode]
            ELSE d1.[DepartmentCode]
        END     AS  [Level1]
    ,   CASE 
            WHEN d4.[DepartmentCode] IS NOT NULL THEN d3.[DepartmentCode]
            WHEN d3.[DepartmentCode] IS NOT NULL THEN d2.[DepartmentCode]
            WHEN d2.[DepartmentCode] IS NOT NULL THEN d1.[DepartmentCode]
            ELSE NULL
        END     AS  [Level2]
    ,   CASE 
            WHEN d4.[DepartmentCode] IS NOT NULL THEN d2.[DepartmentCode]
            WHEN d3.[DepartmentCode] IS NOT NULL THEN d1.[DepartmentCode]           
            ELSE NULL
        END     AS  [Level3]
    ,   CASE 
            WHEN d4.[DepartmentCode] IS NOT NULL THEN d1.[DepartmentCode]           
            ELSE NULL
        END     AS  [Level4]
    ,   d1.[DepartmentCode] AS  [DepartmentCode]    
    ,   d1.[CostCentreCode] AS  [CostCenter]
FROM    [Departments] d1
LEFT JOIN
        [Departments] d2
ON      d1.[Parent] = d2.[DepartmentCode]
LEFT JOIN
        [Departments] d3
ON      d2.[Parent] = d3.[DepartmentCode]
LEFT JOIN
        [Departments] d4
ON      d3.[Parent] = d4.[DepartmentCode]
) AS dept
ON  emp.[Department] = dept.[DepartmentCode]
ORDER BY emp.[ClockNo]
于 2016-04-18T10:58:56.927 回答
2

这里的主要挑战是员工的部门可能需要显示在列Level1Level2Level3Level4中,具体取决于层次结构中该部门有多少上层。

我建议首先在内部查询中查询每个员工的部门级别数,然后使用该信息将部门代码放在右栏中:

SELECT    ClockNo, CostCentre,
          CASE LevelCount
             WHEN 1 THEN Dep1
             WHEN 2 THEN Dep2
             WHEN 3 THEN Dep3
             ELSE        Dep4
          END Level1,
          CASE LevelCount
             WHEN 2 THEN Dep1
             WHEN 3 THEN Dep2
             WHEN 4 THEN Dep3
          END Level2,
          CASE LevelCount
             WHEN 3 THEN Dep1
             WHEN 4 THEN Dep2
          END Level3,
          CASE LevelCount
             WHEN 4 THEN Dep1
          END Level4
FROM      (SELECT   e.ClockNo, e.CostCentre, 
                    CASE WHEN d2.DepartmentCode IS NULL THEN 1
                      ELSE CASE WHEN d3.DepartmentCode IS NULL THEN 2
                        ELSE CASE WHEN d4.DepartmentCode IS NULL THEN 3
                           ELSE 4
                        END
                      END
                    END AS LevelCount,
                    d1.DepartmentCode Dep1, d2.DepartmentCode Dep2,
                    d3.DepartmentCode Dep3, d4.DepartmentCode Dep4
          FROM      Employees e
          LEFT JOIN departments AS d1 ON d1.DepartmentCode = e.Department
          LEFT JOIN departments AS d2 ON d2.DepartmentCode = d1.Parent
          LEFT JOIN departments AS d3 ON d3.DepartmentCode = d2.Parent
          LEFT JOIN departments AS d4 ON d4.DepartmentCode = d3.Parent) AS Base
ORDER BY  ClockNo

SQL小提琴

或者,您可以UNION ALL根据现有级别(0 到 4 个部门的链)对 5 种可能的场景进行简单分析:

SELECT     ClockNo, CostCentre,       d4.DepartmentCode Level1,
           d3.DepartmentCode Level2,  d2.DepartmentCode Level3,
           d1.DepartmentCode Level4
FROM       Employees e
INNER JOIN departments AS d1 ON d1.DepartmentCode = e.Department
INNER JOIN departments AS d2 ON d2.DepartmentCode = d1.Parent
INNER JOIN departments AS d3 ON d3.DepartmentCode = d2.Parent
INNER JOIN departments AS d4 ON d4.DepartmentCode = d3.Parent
UNION ALL
SELECT     ClockNo, CostCentre, d3.DepartmentCode,
           d2.DepartmentCode,   d1.DepartmentCode, NULL
FROM       Employees e
INNER JOIN departments AS d1 ON d1.DepartmentCode = e.Department
INNER JOIN departments AS d2 ON d2.DepartmentCode = d1.Parent
INNER JOIN departments AS d3 ON d3.DepartmentCode = d2.Parent
WHERE      d3.Parent = 0
UNION ALL
SELECT     ClockNo, CostCentre, d2.DepartmentCode,
           d1.DepartmentCode,   NULL, NULL
FROM       Employees e
INNER JOIN departments AS d1 ON d1.DepartmentCode = e.Department
INNER JOIN departments AS d2 ON d2.DepartmentCode = d1.Parent
WHERE      d2.Parent = 0
UNION ALL
SELECT     ClockNo, CostCentre, d1.DepartmentCode Level1,
           NULL, NULL, NULL
FROM       Employees e
INNER JOIN departments AS d1 ON d1.DepartmentCode = e.Department
WHERE      d1.Parent = 0
UNION ALL
SELECT     ClockNo, CostCentre, NULL, NULL, NULL, NULL
FROM       Employees e
WHERE      e.Department = 0
ORDER BY   ClockNo

SQL小提琴

于 2016-04-18T09:36:36.873 回答
1

SunnyMagadan 的查询很好。但是根据部门中的员工数量,您可能希望尝试以下方法,这使 DB 优化器有机会只为一个部门遍历部门层次结构一次,而不是为部门中的每个员工重复它。

SELECT e.ClockNo, e.CostCentre,  Level1, Level2, Level3, Level4
FROM Employees e
LEFT JOIN 
    (SELECT 
         d1.departmentcode
        , d1.CostCentreCode
        , coalesce (d4.departmentcode, d3.departmentcode
                    , d2.departmentcode, d1.departmentcode) AS Level1
        , case when d4.departmentcode is not null then d3.departmentcode        
               when d3.departmentcode is not null then d2.departmentcode
               when d2.departmentcode is not null then d1.departmentcode end as Level2
        , case when d4.departmentcode is not null then d2.departmentcode
               when d3.departmentcode is not null then d1.departmentcode end as Level3
        , case when d4.departmentcode is not null then d1.departmentcode end as Level4
    FROM departments AS d1
    LEFT JOIN departments AS d2 ON d1.parent = d2.departmentcode
    LEFT JOIN departments AS d3 ON d2.parent = d3.departmentcode
    LEFT JOIN departments AS d4 ON d3.parent = d4.departmentcode) d
ON d.DepartmentCode = e.Department AND d.CostCentreCode = e.CostCentre
;

编辑关于 5 级以上的部门。

任何固定步长查询都无法获得前 4 级。所以改变上面的查询只是为了以某种方式标记它们,例如-1。

, case when d4.Parent > 0 then NULL else 
    coalesce (d4.departmentcode, d3.departmentcode
            , d2.departmentcode, d1.departmentcode) end AS Level1

等等。

于 2016-04-19T14:56:23.883 回答
1

试试这个查询。不确定它如何在大数据上展示自己的性能COALESCE

这个想法是建立一个派生的层次结构表,通向每个Department

lev1    lev2    lev3    lev4
1       NULL    NULL    NULL
1       2       NULL    NULL
1       3       NULL    NULL
1       3       5       NULL
4       NULL    NULL    NULL

然后使用最右边的部门加入它Employees。这是完整的查询:

    SELECT
    ClockNo,
    CostCentre,
    lev1,
    lev2,
    lev3,
    lev4
FROM Employees
LEFT JOIN
(
    SELECT
    d1.DepartmentCode AS lev1,
    NULL as lev2,
    NULL as lev3,
    NULL as lev4
    FROM departments AS d1
    WHERE d1.parent=0
    UNION ALL
    SELECT
        d1.DepartmentCode AS lev1,
        d2.DepartmentCode as lev2,
        NULL as lev3,
        NULL as lev4
    FROM departments AS d1
        JOIN departments AS d2 ON d2.parent = d1.departmentcode
    WHERE d1.parent=0
    UNION ALL
    SELECT
        d1.DepartmentCode AS lev1,
        d2.DepartmentCode as lev2,
        d3.DepartmentCode as lev3,
        NULL as lev4
    FROM departments AS d1
        JOIN departments AS d2 ON d2.parent = d1.departmentcode
        JOIN departments AS d3 ON d3.parent = d2.departmentcode
    WHERE d1.parent=0
    UNION ALL
    SELECT
        d1.DepartmentCode AS lev1,
        d2.DepartmentCode as lev2,
        d3.DepartmentCode as lev3,
        d4.DepartmentCode as lev4
    FROM departments AS d1
        JOIN departments AS d2 ON d2.parent = d1.departmentcode
        JOIN departments AS d3 ON d3.parent = d2.departmentcode
        JOIN departments AS d4 ON d4.parent = d3.departmentcode
    WHERE d1.parent=0
) Department
    ON COALESCE(Department.lev4, Department.lev3, Department.lev2, Department.lev1) = Employees.Department
ORDER BY ClockNo
于 2016-04-18T11:01:13.580 回答
0

所以我采取了两个步骤来完成这项工作:

  1. 我不得不递归地为部门生成关卡
  2. 生成所有可能的父节点,以便我可以在透视视图中显示它们

此递归查询构建 DepartmentLevels:

;WITH CTE (DepartmentCode, CostCentreCode, Parent, DepartmentLevel)
AS (
    SELECT D.DepartmentCode, D.CostCentreCode, D.Parent, 1
    FROM dbo.Departments AS D
    WHERE D.Parent = 0
    UNION ALL
    SELECT D.DepartmentCode, D.CostCentreCode, D.Parent, C.DepartmentLevel + 1
    FROM dbo.Departments AS D
    INNER JOIN CTE AS C
        ON C.DepartmentCode = D.Parent
        AND C.CostCentreCode = D.CostCentreCode
    )
SELECT *
INTO #DepartmentLevels
FROM CTE;

这就是输出:

╔════════════════╦════════════════╦════════╦═════════════════╗
║ DepartmentCode ║ CostCentreCode ║ Parent ║ DepartmentLevel ║
╠════════════════╬════════════════╬════════╬═════════════════╣
║              1 ║ AAA            ║      0 ║               1 ║
║              4 ║ BBB            ║      0 ║               1 ║
║              2 ║ AAA            ║      1 ║               2 ║
║              3 ║ AAA            ║      1 ║               2 ║
║              5 ║ AAA            ║      3 ║               3 ║
╚════════════════╩════════════════╩════════╩═════════════════╝

现在这个查询将为每个节点生成所有可能的父节点(一种映射表):

;WITH CTE (DepartmentCode, CostCentreCode, Parent, DepartmentLevelCode)
AS (
    SELECT D.DepartmentCode, D.CostCentreCode, D.Parent, D.DepartmentCode
    FROM dbo.Departments AS D
    UNION ALL
    SELECT D.DepartmentCode, D.CostCentreCode, D.Parent, C.DepartmentLevelCode
    FROM dbo.Departments AS D
    INNER JOIN CTE AS C
        ON C.Parent = D.DepartmentCode
    )
SELECT *
FROM CTE;

这给了我们这个结果:

╔════════════════╦════════════════╦════════╦═════════════════════╗
║ DepartmentCode ║ CostCentreCode ║ Parent ║ DepartmentLevelCode ║
╠════════════════╬════════════════╬════════╬═════════════════════╣
║              1 ║ AAA            ║      0 ║                   1 ║
║              2 ║ AAA            ║      1 ║                   2 ║
║              3 ║ AAA            ║      1 ║                   3 ║
║              4 ║ BBB            ║      0 ║                   4 ║
║              5 ║ AAA            ║      3 ║                   5 ║
║              3 ║ AAA            ║      1 ║                   5 ║
║              1 ║ AAA            ║      0 ║                   5 ║
║              1 ║ AAA            ║      0 ║                   3 ║
║              1 ║ AAA            ║      0 ║                   2 ║
╚════════════════╩════════════════╩════════╩═════════════════════╝

现在我们可以将这三个伙伴与Employeestable 组合在一起并获得所需的输出:

;WITH CTE (DepartmentCode, CostCentreCode, Parent, DepartmentLevelCode)
AS (
    SELECT D.DepartmentCode, D.CostCentreCode, D.Parent, D.DepartmentCode
    FROM dbo.Departments AS D
    UNION ALL
    SELECT D.DepartmentCode, D.CostCentreCode, D.Parent, C.DepartmentLevelCode
    FROM dbo.Departments AS D
    INNER JOIN CTE AS C
        ON C.Parent = D.DepartmentCode
    )
SELECT E.ClockNo
    , E.CostCentre
    , C.Level1
    , C.Level2
    , C.Level3
    , C.Level4
FROM dbo.Employees AS E
OUTER APPLY (
    SELECT MAX(CASE WHEN DL.DepartmentLevel = 1 THEN C.DepartmentCode END)
        , MAX(CASE WHEN DL.DepartmentLevel = 2 THEN C.DepartmentCode END)
        , MAX(CASE WHEN DL.DepartmentLevel = 3 THEN C.DepartmentCode END)
        , MAX(CASE WHEN DL.DepartmentLevel = 4 THEN C.DepartmentCode END)
    FROM CTE AS C
    INNER JOIN #DepartmentLevels AS DL
        ON DL.DepartmentCode = C.DepartmentCode
    WHERE C.DepartmentLevelCode = E.Department
    ) AS C(Level1, Level2, Level3, Level4);

它会给出这个:

╔═════════╦════════════╦════════╦════════╦════════╦════════╗
║ ClockNo ║ CostCentre ║ Level1 ║ Level2 ║ Level3 ║ Level4 ║
╠═════════╬════════════╬════════╬════════╬════════╬════════╣
║       1 ║ AAA        ║        ║        ║        ║        ║
║       2 ║ AAA        ║ 1      ║ 3      ║        ║        ║
║       3 ║ BBB        ║        ║        ║        ║        ║
║       4 ║ BBB        ║ 4      ║        ║        ║        ║
║       5 ║ CCC        ║        ║        ║        ║        ║
║       6 ║ AAA        ║ 1      ║        ║        ║        ║
║       7 ║ AAA        ║ 1      ║ 3      ║ 5      ║        ║
╚═════════╩════════════╩════════╩════════╩════════╩════════╝

此查询将DepartmentLevelCode根据. 希望是对的。DepartmentCodeDepartmentLevel

于 2016-04-18T11:55:00.107 回答
0

我建议您将获取员工和获取他/她的部门层次结构的查询分开。

要获得部门的层次结构,我建议您使用递归 CTE,如下所示:

with DepartmentList (DepartmentCode, CostCentreCode, Parent) AS
(
    SELECT 
        parentDepartment.DepartmentCode, 
        parentDepartment.CostCentreCode, 
        parentDepartment.Parent
    FROM Departments parentDepartment
    WHERE DepartmentCode = @departmentCode

    UNION ALL

    SELECT 
        childDepartment.DepartmentCode
        childDepartment.CostCentreCode,
        childDepartment.Parent,
    FROM Departments childDepartment
    JOIN DepartmentList
    ON childDepartment.Parent = DepartmentList.DepartmentCode
)

SELECT * FROM DepartmentList

这不是您问题的直接答案,但这会给您提供选择和想法。希望这可以帮助。

于 2016-04-18T09:55:19.130 回答