提示
Spark SQL 教程 正在筹划编写中,使用过程中有任何建议,提供意见、建议、纠错、催更加微信 gairuo123。
公共表表达式(CTE)定义了一个临时结果集,用户可以在SQL语句的范围内多次引用该结果集。CTE 主要用于 SELECT 语句中。
Spark SQL 的 WITH 结构为:
WITH common_table_expression [ , ... ]
而 common_table_expression 定义为:
expression_name [ ( column_name [ , ... ] ) ] [ AS ] ( query )
参数:
以下是一些示例讲解:
-- 具有多个列别名的 CTE
WITH t(x, y) AS (SELECT 1, 2)
SELECT * FROM t WHERE x = 1 AND y = 2;
+---+---+
| x| y|
+---+---+
| 1| 2|
+---+---+
-- CTE 定义中的 CTE
WITH t AS (
WITH t2 AS (SELECT 1)
SELECT * FROM t2
)
SELECT * FROM t;
+---+
| 1|
+---+
| 1|
+---+
-- 子查询中的 CTE
SELECT max(c) FROM (
WITH t(c) AS (SELECT 1)
SELECT * FROM t
);
+------+
|max(c)|
+------+
| 1|
+------+
-- 子查询表达式中的 CTE
SELECT (
WITH t AS (SELECT 1)
SELECT * FROM t
);
+----------------+
|scalarsubquery()|
+----------------+
| 1|
+----------------+
-- CREATE VIEW 语句中的 CTE
CREATE VIEW v AS
WITH t(a, b, c, d) AS (SELECT 1, 2, 3, 4)
SELECT * FROM t;
SELECT * FROM v;
+---+---+---+---+
| a| b| c| d|
+---+---+---+---+
| 1| 2| 3| 4|
+---+---+---+---+
-- CREATE VIEW 语句中的 CTE
-- 设置 spark.sql.legacy.ctePrecedencePolicy = CORRECTED (推荐)
-- 内部 CTE 定义优先于外部定义
SET spark.sql.legacy.ctePrecedencePolicy = CORRECTED;
WITH
t AS (SELECT 1),
t2 AS (
WITH t AS (SELECT 2)
SELECT * FROM t
)
SELECT * FROM t2;
+---+
| 2|
+---+
| 2|
+---+
更新时间:2021-08-14 14:35:21 标签:sql spark with cte