PostgreSQL provides the WITH statement, allowing you construct an auxiliary statement for the query. These statements are usually called common table expressions or cte. cte similar temporary table only exists during query execution.
Recursive query refers to the recursive CTE query. Recursive queries are useful in many situations, such as querying the organizational structure, BOM and other hierarchical data
The following shows a recursive CTE syntax:
WITH RECURSIVE cte_name( CTE_query_definition -- non-recursive term UNION [ALL] CTE_query definion -- recursive term ) SELECT * FROM cte_name;
Recursive CTE has three elements:
1. Non-recursive term: Non-recursive queries CTE term is defined, which constitutes the basic result set CTE structure.
Item 2. Recursion: recursive term use or UNION ALL UNION operator to connect one or more defined non-recursive queries CTE item. Recursive CTE reference item name itself.
3. Termination check: When the iteration does not return any rows, recursion will stop.
PostgreSQL perform a recursive CTE in the following order:
1. Perform a basic non-recursive term to create a result set (R0).
2. Ri as a recursive input term, return the result Ri + 1 set as the output.
3. Repeat step 2 until it returns an empty set. (Termination check)
4. Return the final result set, and it is a set of, or all of result sets R0, R1, ...... Rn and set
We will create a new table to demonstrate PostgreSQL recursive queries.
CREATE TABLE employees ( employee_id serial PRIMARY KEY, full_name VARCHAR NOT NULL, manager_id INT );
Employees table consists of three columns: employee_id, manager_id and full name. manager_id column specifies the employee's manager id.
The following exemplary statement data into the employees table.
INSERT INTO employees ( employee_id, full_name, manager_id ) VALUES (1, 'Michael North', NULL), (2, 'Megan Berry', 1), (3, 'Sarah Berry', 1), (4, 'Zoe Black', 1), (5, 'Tim James', 1), (6, 'Bella Tucker', 2), (7, 'Ryan Metcalfe', 2), (8, 'Max Mills', 2), (9, Benjamin Glover ', 2), (10, 'Carolyn Henderson', 3), (11, 'Nicola Kelly', 3), (12 'Alexandra Climo', 3); (13, 'Dominic King', 3), (14, 'Leonard Gray', 4), (15, 'Eric Rampling', 4), (16, 'Piers Paige', 7), (17, 'Ryan Henderson', 7), (18, 'Frank Tucker', 8), (19, 'Nathan Ferguson', 8), (20, 'Kevin Rampling', 8);
The following query returns the id for all subordinate managers 2.
WITH RECURSIVE subordinates AS ( SELECT employee_id, manager_id, full_name FROM employees WHERE employee_id = 2 UNION SELECT e.employee_id, e.manager_id, e.full_name FROM employees e INNER JOIN subordinates s ON s.employee_id = e.manager_id ) SELECT * FROM subordinates;
The above sql works:
1. recursive CTE subordinates defines a non-recursive term and a recursive term.
2. Non-recurrent items return to the basic result set R0, namely id for employees 2.
employee_id | manager_id | full_name -------------+------------+------------- 2 | 1 | Megan Berry
Recursive term return staff id 2 direct reports. This is the result of the connection between the employee table and the subordinates CTE. The first iteration of the recursive term returns this result set:
employee_id | manager_id | full_name -------------+------------+----------------- 6 | 2 | Bella Tucker 7 | 2 | Ryan Metcalfe 8 | 2 | Max Mills 9 | 2 | Benjamin Glover
Repeat PostgreSQL recursive items. Second iteration step using the recursive member result set as input values and returns the result set:
employee_id | manager_id | full_name -------------+------------+----------------- 16 | 7 | Piers Paige 17 | 7 | Ryan Henderson 18 | 8 | Frank Tucker 19 | 8 | Nathan Ferguson 20 | 8 | Kevin Rampling
Third iteration returns an empty result set, because there is no staff to the id for the staff of 16, 17 and 20.
PostgreSQL returns the final result set, the result set is a non-recursive and recursive entry generating first and second sets of all the results and set iteration.
employee_id | manager_id | full_name -------------+------------+----------------- 2 | 1 | Megan Berry 6 | 2 | Bella Tucker 7 | 2 | Ryan Metcalfe 8 | 2 | Max Mills 9 | 2 | Benjamin Glover 16 | 7 | Piers Paige 17 | 7 | Ryan Henderson 18 | 8 | Frank Tucker 19 | 8 | Nathan Ferguson 20 | 8 | Kevin Rampling (10 rows)
In this tutorial, we learned how to use a recursive structure cte PostgreSQL recursive queries.
Reference Address: http: //www.postgresqltutorial.com/postgresql-recursive-query/