Skip to content

To get result from table under hierarchy

To get result from table under hierarchy.

WITH MyCTE
AS ( SELECT Id, ParentId, UserName, 1 as EmplyLevel
FROM User
WHERE User = '[email protected]'
UNION ALL
select E.Id, E.ParentID, E.UserName,EmplyLevel+1
FROM User E
INNER JOIN MyCTE ON MyCTE.EmployeeIndexId = E.SupervisorID
WHERE E.User = '[email protected]'
)
SELECT * FROM MyCTE

Leave a Reply

%d bloggers like this: