Hint /*+ MERGE() */
, otherwise the correct execution plan may not be generated. In subsequent versions, the read-only analysis engine will gradually support recursive CTEs and optimize the CTE execution performance.-- Start defining CTE. WITH CustomerCTE AS ( SELECT customer_id, first_name, last_name, email_address FROM customer ) -- End defining CTE. SELECT * FROM CustomerCTE; -- Reference the CTE.
with_clause:WITH [RECURSIVE]cte_name [(col_name [, col_name] ...)] AS (subquery)[, cte_name [(col_name [, col_name] ...)] AS (subquery)] ...
Parameter | Description |
WITH keyword | Indicates the beginning of the CTE definition. |
[RECURSIVE] | Optional keyword. If RECURSIVE is contained, it indicates that self-referencing within the CTE is allowed. It is used to create recursive queries. |
cte_name | The name specified for the CTE, which can be referenced in subsequent queries. |
[(col_name [,col_name] ...)] | Optional column name list, which specifies the column names for the CTE result sets. If omitted, the column names in the subquery will be used. |
AS (subquery) | Internal subquery of the CTE, which defines the CTE content. |
Comma and additional CTEs | In a WITH clause, you can define multiple CTEs separated by commas. Each additional CTE follows the same structure cte_name [(col_name ...)] AS (subquery). |
WITH cte1 AS (SELECT * FROM t1, t2), cte2 AS (SELECT i1, i2 FROM cte1 WHERE i3 > 10) cte3 AS (SELECT * FROM cte2, t3 WHERE cte2.i1 = t3.i1) SELECT * FROM cte3;
WITH RECURSIVE cte(n, fact) AS (SELECT 0, 1 -- Seed Part SubqueryUNION ALL -- Union TypeSELECT n + 1, (n + 1) * fact FROM cte WHERE n < 5 -- Recursive Part Subquery)SELECT n, fact FROM cte;
WITH RECURSIVE cte(n, fact) AS (SELECT 0, 1UNION ALLSELECT n + 1, (n + 1) * fact FROM cte WHERE n < 5)SELECT n, fact FROM cte;
UNION ALL SELECT n + 1, (n + 1) * fact FROM cte WHERE n < 5
will repeatedly call itself until n reaches 5. The recursion ends when the recursive part outputs an empty row.CREATE TABLE employees (id INT PRIMARY KEY,name VARCHAR(100),manager_id INT);INSERT INTO employees (id, name, manager_id) VALUES(1, 'CEO', NULL),(2, 'Manager 1', 1),(3, 'Manager 2', 1),(4, 'Employee 1', 2),(5, 'Employee 2', 2),(6, 'Employee 3', 3);A recursive CTE is used to traverse the employee hierarchy, retrieving all subordinates from top to bottom:WITH RECURSIVE employee_hierarchy AS (-- Basic situation: starting from CEOSELECTid,name,manager_id,1 AS levelFROM employeesWHERE manager_id IS NULLUNION ALL-- Recursive situation: finding each employee's subordinatesSELECTe.id,e.name,e.manager_id,eh.level + 1FROM employees eINNER JOIN employee_hierarchy eh ON eh.id = e.manager_id)SELECT id, name, manager_id, levelFROM employee_hierarchyORDER BY level, manager_id;-- Result┌───────┬────────────┬────────────┬───────┐│ id │ name │ manager_id │ level ││ int32 │ varchar │ int32 │ int32 │├───────┼────────────┼────────────┼───────┤│ 1 │ CEO │ │ 1 ││ 2 │ Manager 1 │ 1 │ 2 ││ 3 │ Manager 2 │ 1 │ 2 ││ 4 │ Employee 1 │ 2 │ 3 ││ 5 │ Employee 2 │ 2 │ 3 ││ 6 │ Employee 3 │ 3 │ 3 │└───────┴────────────┴────────────┴───────┘
WITH CustomerCTE AS (SELECT customer_id, first_name, last_name, email_addressFROM customer)SELECT /*+ MERGE() */ *FROM CustomerCTE;
WITHCTE1 AS (SELECT customer_id, first_name, last_name, email_addressFROM customer),CTE2 AS (SELECT ss_item_sk, ss_customer_sk, ss_sold_date_sk, ss_sales_priceFROM store_sales)SELECT /*+ MERGE() */ CTE1.first_name, CTE1.last_name, CTE2.ss_sales_priceFROM CTE1JOIN CTE2 ON CTE1.customer_id = CTE2.ss_customer_sk;
+------------+-----------+----------------+| first_name | last_name | ss_sales_price |+------------+-----------+----------------+| John | Doe | 45.99 || Jane | Smith | 32.50 || Michael | Johnson | 78.25 || Emily | Brown | 19.99 || David | Wilson | 55.00 || John | Doe | 67.75 || Jane | Smith | 22.99 || Michael | Johnson | 41.50 || Emily | Brown | 89.99 || David | Wilson | 33.25 |+------------+-----------+----------------+10 rows in set (0.12 sec)
WITH SalesSummary AS (SELECT ss_customer_sk, SUM(ss_net_paid) AS total_spentFROM store_salesGROUP BY ss_customer_sk),TopCustomers AS (SELECT ss_customer_sk, total_spentFROM SalesSummaryWHERE total_spent > 1000 -- Assume that a threshold is set, such as customer spending more than 1,000.),CustomerDetails AS (SELECT c.customer_id, c.first_name, c.last_name, tc.total_spentFROM customer cJOIN TopCustomers tc ON c.customer_id = tc.ss_customer_sk)SELECT /*+ MERGE() */ *FROM CustomerDetails;
+-------------+------------+-----------+--------------+| customer_id | first_name | last_name | total_spent |+-------------+------------+-----------+--------------+| 1001 | John | Doe | 1523.75 || 1002 | Jane | Smith | 2105.50 || 1003 | Michael | Johnson | 1789.99 || 1004 | Emily | Brown | 1650.25 || 1005 | David | Wilson | 1875.00 || 1006 | Sarah | Davis | 2250.75 || 1007 | Robert | Taylor | 1955.50 || 1008 | Jennifer | Anderson | 1725.25 || 1009 | William | Thomas | 2015.00 || 1010 | Lisa | Jackson | 1890.75 |+-------------+------------+-----------+--------------+10 rows in set (0.15 sec)
Was this page helpful?