Hint /*+ MERGE() */
,否则可能无法生成正确的执行计划。在后续版本,只读分析引擎将逐步支持 Recursive CTE,且会优化 CTE 的执行性能。-- 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)] ...
参数项 | 描述 |
WITH 关键字 | 表示 CTE 定义的开始。 |
[RECURSIVE] | 可选关键字,如果包含 RECURSIVE,表示允许在 CTE 中引用自身,用于创建递归查询。 |
cte_name | 为 CTE 指定的名称,可以在后续的查询中被引用。 |
[(col_name [,col_name] ...)] | 可选的列名列表,为 CTE 的结果集指定列名。如果省略,将使用子查询中的列名。 |
AS (subquery) | CTE 内部的子查询,定义 CTE 的内容。 |
逗号和额外的 CTEs | 在一个 WITH 子句中,可以定义多个 CTEs,用逗号分隔。每个额外的 CTE 都遵循相同的结构: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
会重复调用自身,直到 n 达到5,递归部分输出空行时,结束递归。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);递归 CTE 用于遍历员工层次结构,从上到下获取所有下属:WITH RECURSIVE employee_hierarchy AS (-- 基础情况:从 CEO 开始SELECTid,name,manager_id,1 AS levelFROM employeesWHERE manager_id IS NULLUNION ALL-- 递归情况:找到每个员工的下属SELECTe.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 -- 假设设置一个阈值, 例如消费超过 1000 的顾客),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)
本页内容是否解决了您的问题?