Recursive SQL with CTE

Latest post 11-13-2009 4:43 PM by Pat. 1 replies.
  • 05-13-2009 3:32 PM

    • Pat
    • Top 10 Contributor
      Male
    • Joined on 04-27-2008
    • Tempe, AZ
    • Posts 45

    Recursive SQL with CTE

     Build hierarchy using CTE and recursion

    http://msdn.microsoft.com/en-us/library/ms186243.aspx

    WITH DirectReports ([ManagerID], [EmployeeID], [RoleID], [DepartmentID], Level)

    AS

    (

    -- Root member definition

    SELECT e.[ManagerID], e.[EmployeeID], e.[RoleID], e.[DepartmentID], 0 AS Level

        FROM [dbo].[Employees] AS e where e.[RecordEnd] is null and [ManagerID] = -1

          UNION ALL

    -- Recursive member definition

        SELECT e.[ManagerID], e.[EmployeeID], e.[RoleID], e.[DepartmentID], Level + 1

        FROM [dbo].[Employees] AS e

        INNER JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID

          where e.[RecordEnd] is null

    )

    -- Statement that executes the CTE

    SELECT [ManagerID], [EmployeeID], [RoleID], [DepartmentID], Level

    FROM DirectReports

     

     

     

    Count how many employees are at each level of the hierarchy

    SELECT distinct(a.Level),

    (Select count(b.Level) FROM DirectReports as b where a.level = b.level) as levelcount

    FROM DirectReports as a

     

     

    Patrick McNamara, BS-IS/CS, MBA, MAED
    ASP.NET Web Application Developer
    Asteryx, LLC.
    http://asteryx.com
    pat@asteryx.com

  • 11-13-2009 4:43 PM In reply to

    • Pat
    • Top 10 Contributor
      Male
    • Joined on 04-27-2008
    • Tempe, AZ
    • Posts 45

    Re: Recursive SQL with CTE

     Here is a query that uses a CTE to build an employee hierarchy and will display how many levels of employees are below a manager. 

    WITH EmployeeHierarchy (EmployeeID, LastName, FirstName, ManagerID, HierarchyLevel) AS
    (
       -- Base case
       SELECT
          EmployeeID,
          LastName,
          FirstName,
          ManagerID,
          0 as HierarchyLevel
       FROM dbo.Employees
       WHERE RecordEnd is null
     AND EmployeeID NOT IN (SELECT DISTINCT ManagerID FROM dbo.Employees WHERE RecordEnd is null)

       UNION ALL

       -- Recursive step
       SELECT
         e.EmployeeID,
          e.LastName,
          e.FirstName,
          e.ManagerID,
          eh.HierarchyLevel + 1 AS HierarchyLevel
       FROM dbo.Employees e
          INNER JOIN EmployeeHierarchy eh ON
             e.EmployeeID = eh.ManagerID and e.ManagerID <> -1
     Where e.RecordEnd is null
    )

    SELECT DISTINCT eh.EmployeeID,
          eh.LastName,
          eh.FirstName,
          eh.ManagerID,
         eh.HierarchyLevel
    FROM  EmployeeHierarchy EH
    INNER JOIN (SELECT  employeeid, MAX(HierarchyLevel) AS 'Level'
                            FROM EmployeeHierarchy group by employeeid) MaxLevel
    ON EH.EmployeeID=MaxLevel.EmployeeID AND EH.HierarchyLevel=MaxLevel.Level
    ORDER BY lastname, firstname

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    Patrick McNamara, BS-IS/CS, MBA, MAED
    ASP.NET Web Application Developer
    Asteryx, LLC.
    http://asteryx.com
    pat@asteryx.com

Page 1 of 1 (2 items) | RSS
Forums to discuss Microsoft ASP.Net Development and SQL
Powered by Community Server (Non-Commercial Edition), by Telligent Systems