从 BigQuery 中的先前 WITH 子句创建 table

Creating a table from a prior WITH clause in BigQuery

WITH LAYER AS (
   SELECT 
      SPLIT(de_nest, '|')[OFFSET(1)] AS product,
      ....
      

   FROM `table`,
   UNNEST(SPLIT(LOWER(REPLACE(variable, '^', '-')), '-')) AS de_nest
) 
-- Filter out empty products
CREATE OR REPLACE TABLE `newtable` AS
SELECT * FROM LAYER WHERE product is NOT NULL

这导致我出现以下错误。

Syntax error: Expected "(" or "," or keyword SELECT but got keyword CREATE at [25:1]

但我似乎找不到解决这个问题的合理方法。我的第一个工作负载是对第一个 table 进行取消嵌套,第二个工作是对从取消嵌套过程生成的那些列进行一些过滤。

您应该尝试将 CTE 声明放在 CREATE 语句之后:

CREATE OR REPLACE TABLE `new_table` AS
WITH layer AS ...

编辑:一个完整​​的例子

CREATE OR REPLACE TABLE
  `your_project.your_dataset.your_table` AS
WITH
  layer1 AS (
  SELECT
    'this is my CTE' AS txt),
  another_cte AS (
  SELECT
    txt,
    SPLIT(txt, ' ') AS my_array
  FROM
    layer1)
SELECT
  *
FROM
  another_cte

创建以下 table