Common Table Expression
Descriptionβ
Common Table Expression (CTE) define a temporary result set that can be referenced multiple times within the scope of an SQL statement. CTE are primarily used in SELECT statements.
To specify a CTE, use the WITH
clause with one or more comma-separated clauses. Each clause provides a subquery that generates a result set and associates a name with the subquery.
Doris supports nested CTEγ Within the statement that contains the WITH
clause, you can reference each CTE name to access the corresponding CTE result set. CTE names can be referenced in other CTE, allowing you to define CTE based on other CTE.
Doris DOES NOT support recursive CTE. For more information, please read MySQL manual about recursive CTE
Exampleβ
Simple CTEβ
The following example defines CTE named cte1 and cte2 within the WITH clause and refers to them in the top-level SELECT below the WITH clause:
WITH
cte1 AS (SELECT a, b FROM table1),
cte2 AS (SELECT c, d FROM table2)
SELECT b, d FROM cte1 JOIN cte2
WHERE cte1.a = cte2.c;
Nested CTEβ
WITH
cte1 AS (SELECT a, b FROM table1),
cte2 AS (SELECT c, d FROM cte1)
SELECT b, d FROM cte1 JOIN cte2
WHERE cte1.a = cte2.c;
Recursive CTE (NOT Support)β
WITH r_cte AS (
SELECT 1 AS user_id, 2 as manager_id
UNION ALL
SELECT user_id, manager_id FROM r_cte INNER JOIN (SELECT 1 AS user_id, 2 as manager_id) t ON r_cte.manager_id = t.user_id
)
SELECT * FROM r_cte