Impala - 在 WITH 子句后创建 TABLE

Impala - CREATE TABLE after a WITH clause

我有一个包含多个 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
        )
    )

但是我有以下错误:

Encountered: CREATE Expected: SELECT, VALUES, WITH CAUSED BY: Exception: Syntax error

所以我试着把 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: Could not resolve table reference: 'TABLE_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 列——这应该会产生另一个错误。实际上,您应该 select 正是您想要的列。

或者,您也可以...

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