Рекурсивное общее табличное выражение T SQL - поместить все связанные элементы в столбцы в соответствии с уровнями - PullRequest
0 голосов
/ 16 июня 2020

В настоящее время у меня есть эта таблица, взятая со страницы документов T SQL https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-ver15

+------------+-----------+----------+------------------------------+--------+-----------+
| EmployeeID | FirstName | LastName |            Title             | DeptID | ManagerID |
+------------+-----------+----------+------------------------------+--------+-----------+
|          1 | Ken       | Sánchez  | Chief Executive Officer      |     16 | NULL      |
|         16 | David     | Bradley  | Marketing Manager            |      4 | 273       |
|         23 | Mary      | Gibson   | Marketing Specialist         |      4 | 16        |
|        273 | Brian     | Welcker  | Vice President of Sales      |      3 | 1         |
|        274 | Stephen   | Jiang    | North American Sales Manager |      3 | 273       |
|        275 | Michael   | Blythe   | Sales Representative         |      3 | 274       |
|        276 | Linda     | Mitchell | Sales Representative         |      3 | 274       |
|        285 | Syed      | Abbas    | Pacific Sales Manager        |      3 | 273       |
|        286 | Lynn      | Tsoflias | Sales Representative         |      3 | 285       |
+------------+-----------+----------+------------------------------+--------+-----------+

Я хочу создать таблицу, которая отображала бы всех иерархически более высоких сотрудников в столбцах вот так:

+-----------+------------+------------------------------+---------------+------+------+------+------+
| ManagerID | EmployeeID |            Title             | EmployeeLevel | lvl0 | lvl1 | lvl2 | lvl3 |
+-----------+------------+------------------------------+---------------+------+------+------+------+
| NULL      |          1 | Chief Executive Officer      |             0 |    0 |    0 |    0 |    0 |
| 1         |        273 | Vice President of Sales      |             1 |    1 |    0 |    0 |    0 |
| 273       |         16 | Marketing Manager            |             2 |    1 |  273 |    0 |    0 |
| 273       |        274 | North American Sales Manager |             2 |    1 |  273 |    0 |    0 |
| 273       |        285 | Pacific Sales Manager        |             2 |    1 |  273 |    0 |    0 |
| 285       |        286 | Sales Representative         |             3 |    1 |  273 |  285 |    0 |
| 274       |        275 | Sales Representative         |             3 |    1 |  273 |  274 |    0 |
| 274       |        276 | Sales Representative         |             3 |    1 |  273 |  274 |    0 |
| 16        |         23 | Marketing Specialist         |             3 |    1 |  273 |   16 |    0 |
+-----------+------------+------------------------------+---------------+------+------+------+------+

Я действительно даже не знаю, как go об этом, помимо того, что уже написано на странице документации. Любая помощь будет оценена. Спасибо.

1 Ответ

0 голосов
/ 16 июня 2020

У вас есть фиксированное количество столбцов, поэтому вы можете сделать это, используя join s. Думаю, это лог c:

select t.*,
       (case when t.ManagerId is null then 0
             when tp.ManagerId is null then 1
             when tpp.ManagerId is null then 2
             when tppp.ManagerId is null then 3
        end) as employeelevel,
       coalesce(tppp.ManagerId, tpp.ManagerId, tp.ManagerId, t.ManagerId) as lvl0,
       (case when t.ManagerId is null then 0
             when tp.ManagerId is null then coalesce(t.ManagerId, 0)
             when tpp.ManagerId is null then coalesce(tp.ManagerId, 0)
             when tppp.ManagerId is null then coalesce(tpp.ManagerId, 0)
        end) as lvl1,
       (case when t.ManagerId is null then 0
             when tp.ManagerId is null then 0
             when tpp.ManagerId is null then coalesce(t.ManagerId, 0)
             when tppp.ManagerId is null then coalesce(tp.ManagerId, 0)
        end) as lvl2,
       (case when t.ManagerId is null then 0
             when tp.ManagerId is null then 0
             when tpp.ManagerId is null then 0
             when tppp.ManagerId is null then coalesce(t.ManagerId, 0)
        end) as lvl3
from t left join
     t tp
     on t.ManagerId = tp.EmployeeID left join
     t tpp
     on tp.ManagerId = tpp.EmployeeID left join
     t tppp
     on tpp.ManagerId = tppp.EmployeeID ;
...