Lädt...


🔧 Recursive CTE PART 2


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Let’s create a complete example using a recursive CTE with sample data, including table creation, data insertion, and the recursive query itself.

Step 1: Create the Sample Table

First, we'll create a simple employees table to hold our employee data.

CREATE TABLE employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
ManagerID INT,
FOREIGN KEY (ManagerID) REFERENCES employees(EmployeeID)
);

Step 2: Insert Sample Data

Now, we will insert some sample data into the employees table.

INSERT INTO employees (EmployeeID, Name, ManagerID) VALUES
(1, 'Alice', NULL), -- Alice is the top-level manager
(2, 'Bob', 1), -- Bob reports to Alice
(3, 'Carol', 1), -- Carol reports to Alice
(4, 'Dave', 2), -- Dave reports to Bob
(5, 'Eve', 2), -- Eve reports to Bob
(6, 'Frank', 3), -- Frank reports to Carol
(7, 'Grace', 3); -- Grace reports to Carol

Step 3: Recursive CTE to Retrieve Employee Hierarchy

Now we will write the recursive CTE to retrieve the hierarchy of employees, starting from Alice (the top-level manager).

WITH RECURSIVE EmployeeHierarchy AS (
-- Anchor member: Select the top-level manager (Alice)
SELECT EmployeeID, Name, ManagerID, 0 AS Level
FROM employees
WHERE ManagerID IS NULL

UNION ALL

-- Recursive member: Select employees reporting to the current level's employees
SELECT e.EmployeeID, e.Name, e.ManagerID, eh.Level + 1
FROM employees e
INNER JOIN EmployeeHierarchy eh ON e.ManagerID = eh.EmployeeID

)
-- Final selection: Get the entire hierarchy
SELECT EmployeeID, Name, ManagerID, Level
FROM EmployeeHierarchy
ORDER BY Level, EmployeeID;

Explanation of the CTE:

  1. Anchor Member:

Selects the employee who has no manager (ManagerID IS NULL), which is Alice in this case.

It also includes a Level column to indicate the hierarchy level (0 for Alice).

  1. Recursive Member:

Selects employees whose ManagerID matches the EmployeeID from the previous result set.

It increments the Level by 1 to indicate how deep in the hierarchy the employee is.

  1. Final Selection:

Retrieves the results from the EmployeeHierarchy CTE, ordering by level and employee ID.

Output

When you run the above CTE, the output will look like this:

Summary

Level 0: Alice (the top-level manager)

Level 1: Bob and Carol (direct reports to Alice)

Level 2: Dave and Eve (reports to Bob) and Frank and Grace (reports to Carol)

This example demonstrates how to use a recursive CTE to traverse a hierarchical structure in SQL.

...

🕵️ http://cte.ufmg.br/cte/


📈 50.9 Punkte
🕵️ Hacking

🔧 Recursive Common Table Expression (CTE) PART 1


📈 48.98 Punkte
🔧 Programmierung

🔧 Recursive CTE PART 2


📈 48.98 Punkte
🔧 Programmierung

🔧 CTE Example in SQL


📈 25.45 Punkte
🔧 Programmierung

🔧 CTE in SQL Basic


📈 25.45 Punkte
🔧 Programmierung

🔧 CTE


📈 25.45 Punkte
🔧 Programmierung

🔧 CTE


📈 25.45 Punkte
🔧 Programmierung

🔧 CTE


📈 25.45 Punkte
🔧 Programmierung

📰 CTE Vs. Subqueries In SQL — 3 Practical Tips To Make A Right Choice


📈 25.45 Punkte
🔧 AI Nachrichten

📰 Thermaltake CTE E660 MX: (K)ein Glaskasten-Gehäuse hat auch hinten drei Lüfter


📈 25.45 Punkte
📰 IT Nachrichten

🕵️ CVE-2023-40610 | Apache Superset up to 2.1.2 CTE authorization


📈 25.45 Punkte
🕵️ Sicherheitslücken

📰 Thales CTE-RWP protects critical files and folders from ransomware attacks


📈 25.45 Punkte
📰 IT Security Nachrichten

🔧 CTE


📈 25.45 Punkte
🔧 Programmierung

🔧 CTE | Common Table Expression | Real world scenarios examples


📈 25.45 Punkte
🔧 Programmierung

🔧 Mastering Binary Search in JavaScript Part I: Iterative, Recursive. Day 2 of 30 Days of DSA 🚀🦾


📈 23.53 Punkte
🔧 Programmierung

🕵️ ISC BIND up to 9.8.x Recursive Query Processor denial of service


📈 17.71 Punkte
🕵️ Sicherheitslücken

🔧 Mastering Recursive Types in TypeScript: Handling Depth Limitations Gracefully


📈 17.71 Punkte
🔧 Programmierung

🔧 Exploring Recursive Nets


📈 17.71 Punkte
🔧 Programmierung

🔧 The issue of recursive module calls in declarative infrastructure-as-code


📈 17.71 Punkte
🔧 Programmierung

🐧 chmod 755 ,775 , recursive & ssh permissions Chmod 775 vs 777


📈 17.71 Punkte
🐧 Linux Tipps

🔧 How to Approach Recursive Functions in Your Coding Assignments


📈 17.71 Punkte
🔧 Programmierung

🕵️ ISC BIND up to 9.3.2 INSIST Multiple Recursive Queries denial of service


📈 17.71 Punkte
🕵️ Sicherheitslücken

📰 Exploring Recursive Art: Fractals with Context Free


📈 17.71 Punkte
🔧 AI Nachrichten

🔧 Beware of recursive signals in Django


📈 17.71 Punkte
🔧 Programmierung

📰 Using Recursive Grep to Test Per-Request CSRF-Token Protected Pages


📈 17.71 Punkte
📰 IT Security Nachrichten

🐧 Anyone else constantly find themselves checking man to see if it's -r or -R for recursive?


📈 17.71 Punkte
🐧 Linux Tipps

🔧 Understanding Recursive Queries in PostgreSQL: A Process Hierarchy Example


📈 17.71 Punkte
🔧 Programmierung

🔧 Learning Rust by Parsing Regex with Recursive Descent


📈 17.71 Punkte
🔧 Programmierung

🔧 Managing Recursive Components in React With Custom Hook


📈 17.71 Punkte
🔧 Programmierung

🔧 Creating a Recursive List Menu Any Number of Levels Deep in React


📈 17.71 Punkte
🔧 Programmierung

matomo