-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRecursion.sql
More file actions
80 lines (68 loc) · 1.59 KB
/
Recursion.sql
File metadata and controls
80 lines (68 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
WITH EmployeeHierarchy (employee_num,FullName,Supervisor) AS
(
-- Base case
SELECT Employee_num,
FullName,
Supervisor
FROM tAllEmployee
WHERE Supervisor = 'Hancock,Rex'
UNION ALL
-- Recursive step
SELECT e.employee_Num,
e.FullName,
eh.FullName as Supervisor
FROM tAllEmployee e
INNER JOIN EmployeeHierarchy eh ON
e.Supervisor = eh.FullName
)
-- Recursive Down
;WITH cteTable (colLevel, colID, colParentID, col1, col2) -- Creates the "temporary" table to store the recursive data
AS
(
-- Anchor Statement
SELECT 0 as ColLevel -- artificial number to tell at what level you are. 0 begin top level
, t1.colID
, t1.ColParentID
, t1.col1
, t1.col2
FROM tableName t1
WHERE t1.colID = <someValue>
UNION ALL
-- Recursion Statement
SELECT
cteTable.colLevel + 1 -- increases the further down you go.
, t2.colID
, t2.ColParentID
, t2.col1
, t2.col2
FROM tableName t2
INNER JOIN cteTable ON t2.colParentID = cteTable.colID
)
SELECT colLevel, colID, colParentID, col1, col2
FROM cteTable;
-- Recursive Down
;WITH cteEmployees (EmployeeLevel, EmployeeID, ReportsTo, LastName, FirstName)
AS
(
SELECT 0 AS EmployeeLevel
, EmployeeID
, ReportsTo
, LastName
,FirstName
FROM dbo.Employees e1
WHERE e1.EmployeeID = ?
UNION ALL
SELECT cte.EmployeeLevel + 1
, EmployeeID
, ReportsTo
, LastName
, FirstName
FROM dbo.Employees e2
INNER JOIN cteEmployees cte ON e2.ReportsTo = cte.EmployeeID
)
SELECT EmployeeLevel
, EmployeeID
, ReportsTo
, LastName
, FirstName
FROM cteEmployees;