在视图中嵌入多个CTE

时间:2017-05-16 20:38:22

标签: sql hadoop apache-spark hive

Hive是否支持VIEW语句中的多个CTE?我知道Hive支持CTE(HIVE-1180),但不确定它是否有任何限制 - 例如,支持VIEW语句中的多个CTE:

CREATE OR REPLACE VIEW prdviews.temp
AS WITH set1 AS (SELECT 1 AS id FROM testdb.table1), 
        set2 AS (select 2 AS id FROM testdb.table2)
SELECT * FROM set1
UNION ALL
SELECT * FROM set2;

select * from prdviews.temp;

执行上述查询会成功创建Hive视图。但是当我运行SELECT查询时,它会返回错误。或者我做错了吗?上面的查询或任何类似的查询返回类似于此的错误:

Error while compiling statement: FAILED: SemanticException Line 3:24 Table not found 'set1' in definition of VIEW temp [ WITH set1 AS (SELECT 1 AS `id` FROM `testdb`.`table1`), set2 AS (select 2 AS `id` FROM `testdb`.`table2`) SELECT `set1`.`id` FROM set1 UNION ALL SELECT `set2`.`id` FROM set2 ] used as temp at Line 1:14

1 个答案:

答案 0 :(得分:0)

以下内容适用于CDH 5x

with t1 as(select * from empinfo where empid=101),t2 as(select * from empinfo where empid=102) select * from t1 union all select * from t2;

还有下面的

select * from empinfo where empid=101 union all select * from empinfo where empid=102;