Working with recursive queries

Hierarchical and Recursive Queries in SQL Server

Jasmin Ludolf

Content Developer

The hierarchy of an IT-organization

The organization is described by:

  • ID - Employee ID
  • Name of Employee
  • JobTitle in the company
  • Department in the company
  • Supervisor in the company

Fields describing hierarchy:

  • ID
  • Supervisor

Employee 2 has Supervisor 1

Hierarchical and Recursive Queries in SQL Server

The IT-organization

+-----------------------+---------------------+------------+------------+
|ID | Name              | Position            | Department | Supervisor |    
|-----------------------|---------------------|------------|------------|
| 1 | Heinz Griesser    | IT Director         | IT         | 0          |
| 2 | Andreas Sitter    | Security Manager    | IT         | 1          |
| 3 | Thomas Bergman    | Innovation Manager  | IT         | 1          |
| 4 | Hannes Berg       | Operation Manager   | IT         | 1          |
| 5 | Anna Kruggel      | Administrator       | IT         | 4          |
| 6 | Karin Pacher      | Developer           | IT         | 4          |
+-----------------------+---------------------+------------+------------+
Hierarchical and Recursive Queries in SQL Server

Common tasks for hierarchical data

Get the hierarchy of a record

Who is your supervisor?


Get the level of the hierarchy

Get the hierarchy level of an organization


Combine recursion results into one field

Which supervisors do I have?

Hierarchical and Recursive Queries in SQL Server

Get the hierarchy

WITH hierarchy AS (
    SELECT ID,Supervisor
        FROM employee 
        WHERE supervisor = 0

UNION ALL SELECT emp.ID, emp.Supervisor FROM employee emp
JOIN hierarchy ON emp.Supervisor = hierarchy.ID) SELECT * FROM hierarchy
Hierarchical and Recursive Queries in SQL Server

Get the hierarchy

+---+-----------+
|ID |Supervisor |
|---|-----------|
|1  | 0         |
|2  | 1         |
|3  | 2         |
+---+-----------+
Hierarchical and Recursive Queries in SQL Server

Get the level of the hierarchy

WITH hierarchy AS (
    SELECT ID, Supervisor, 1 as LEVEL
        FROM employee
           WHERE Supervisor = 0

UNION ALL SELECT emp.ID, emp.Supervisor, LEVEL + 1 FROM employee emp
JOIN hierarchy ON emp.Supervisor = hierarchy.ID ) SELECT * FROM hierarchy
Hierarchical and Recursive Queries in SQL Server

Get the level of the hierarchy

+---+-----------+-------|
|ID |Supervisor | Level |
|---|-----------|-------|
|1  | 0         | 0     |
|2  | 1         | 1     |
+---+-----------+-------+
Hierarchical and Recursive Queries in SQL Server

Combine recursion results into one field

WITH hierarchy AS (
    SELECT ID, Supervisor, CAST('0' AS VARCHAR(MAX)) as PATH
        FROM employee
        WHERE Supervisor = 0

UNION ALL SELECT emp.ID, emp.Supervisor, Path + '->' + CAST(emp.Supervisor AS VARCHAR(MAX)) FROM employee emp INNER JOIN hierarchy ON emp.Supervisor = hierarchy.ID ) SELECT * FROM hierarchy
Hierarchical and Recursive Queries in SQL Server

Combine recursion results into one field

+-----+--------+
| PATH         |
|--------------|
| 0 -> 1 -> 4  |
+-----+--------+
Hierarchical and Recursive Queries in SQL Server

Let's query the IT-organization

Hierarchical and Recursive Queries in SQL Server

Preparing Video For Download...