Quantcast
Viewing all articles
Browse latest Browse all 207

SQL Server CTE Bottom to Top Recursive with Where clause

I have an Employee Table with an EmployeeId, ManagerId and a Name field.

The goal is to make a recursive With fetching all the rows from an employee to the top manager (ManagerId is null).

I found this link which helped to get the base of the code but I do not manage to make it work for my case

DECLARE @EmployeeTable table ([EmployeeId] int, [name] varchar(10), [managerId] int)INSERT @EmployeeTable VALUES (1,'Jerome', NULL )  -- tree is as follows:INSERT @EmployeeTable VALUES (2,'Joe'   ,1)     --                      1-JeromeINSERT @EmployeeTable VALUES (3,'Paul'  ,2)     --                     /        \INSERT @EmployeeTable VALUES (4,'Jack'  ,3)     --              2-Joe               9-BillINSERT @EmployeeTable VALUES (5,'Daniel',3)     --            /       \                  \INSERT @EmployeeTable VALUES (6,'David' ,2)     --     3-Paul          6-David       10-SamINSERT @EmployeeTable VALUES (7,'Ian'   ,6)     --    /      \            /    \INSERT @EmployeeTable VALUES (8,'Helen' ,6)     -- 4-Jack  5-Daniel   7-Ian    8-HelenINSERT @EmployeeTable VALUES (9,'Bill ' ,1)     --INSERT @EmployeeTable VALUES (10,'Sam'  ,9)     --DECLARE @employeeId int = 3;WITH StaffTree AS(    SELECT         c.[EmployeeId], c.[name], c.managerId, 0 AS [Level]        FROM @EmployeeTable c        LEFT OUTER JOIN @EmployeeTable  cc ON c.managerId=cc.EmployeeId        WHERE c.EmployeeId=@employeeId OR (@employeeId IS NULL AND     c.managerId IS NULL)    UNION ALL        SELECT             s.[EmployeeId], s.[name], s.managerId, t.[Level]+1        FROM StaffTree t            INNER JOIN @EmployeeTable s ON t.[EmployeeId]=s.managerId        WHERE s.managerId=@employeeId OR @employeeId IS NULL OR t.Level>1)SELECT * FROM StaffTree

In case you select the employee 3 hierarchy, the result should be:

EmployeeId | Name    | ManagerId 1          | Jerome  | NULL2          | Joe     | 13          | Paul    | 2

Viewing all articles
Browse latest Browse all 207

Trending Articles