Impala-WITH子句之后的CREATE TABLE

时间:2019-02-15 12:18:06

标签: sql create-table impala with-statement

我有一个带有多个WITH子句的查询,然后是一个CREATE TABLE

    WITH TABLE_1 AS (
        SELECT * FROM SOMEWHERE_1
    ), TABLE_2 AS (
        SELECT * FROM SOMEWHERE_2

    (
        CREATE TABLE TABLE_3 AS
        (
            SELECT TABLE_1.*, TABLE_2.*
            FROM TABLE_1 
            INNER JOIN TABLE_2 ON TABLE_2.key = TABLE_1.key
        )
    )

但是我遇到以下错误:

  

遇到:创建期望:选择,值和原因:异常:语法错误

所以我尝试将CREATE语句放在第一位:

    CREATE TABLE_3 AS
    (
        WITH TABLE_1 AS (
            SELECT * FROM SOMEWHERE_1
        ), TABLE_2 AS (
            SELECT * FROM SOMEWHERE_2

        (
            SELECT TABLE_1.*, TABLE_2.*
            FROM TABLE_1 
            INNER JOIN TABLE_2 ON TABLE_2.key = TABLE_1.key
        )
    )

但是现在我遇到以下错误:

  

AnalysisException:无法解析表引用:'TABLE_1'

请注意:

  • 上面的查询在没有“ CREATE”语句的情况下起作用
  • 我现在的情况比这个simpe示例要复杂得多,为了清楚起见,我想保留WITH语句。

2 个答案:

答案 0 :(得分:1)

嗯。我认为这会起作用:

CREATE TABLE TABLE_3 AS
    WITH TABLE_1 AS (
          SELECT * FROM SOMEWHERE_1
         ),
         TABLE_2 AS (
          SELECT * FROM SOMEWHERE_2
         )
    SELECT TABLE_1.*, TABLE_2.*
    FROM TABLE_1 INNER JOIN
         TABLE_2
         ON TABLE_2.key = TABLE_1.key;

当然,您还会遇到其他问题,例如结果中的key列重复出现-这会产生另一个错误。实际上,您应该准确选择所需的列。

答案 1 :(得分:0)

或者,您也可以...

WITH TABLE_1 AS (
      SELECT * FROM SOMEWHERE_1
     ),
     TABLE_2 AS (
      SELECT * FROM SOMEWHERE_2
     )
SELECT TABLE_1.*, TABLE_2.* INTO TABLE_3
FROM TABLE_1 INNER JOIN
     TABLE_2
     ON TABLE_2.key = TABLE_1.key

强烈建议始终准备好DDL并运行INSERT INTO TABLE SELECT * FROM CTE