Bigquery多个WITH子句抛出错误

时间:2016-09-26 23:40:22

标签: google-bigquery

我使用BigQuery运行带有多个WITH子句的查询,如下所示:

WITH subq_L1 AS (
    SELECT
        id,
        metric,
        COUNT(DISTINCT IF(DATEDIFF(TIMESTAMP('{execution_date:s}'), TIMESTAMP(ds)) < 1, ds, NULL)) AS L1
    FROM
        [xxx:yyy.zzz]
    GROUP EACH BY
        id, metric
),
subq_L7 AS (
    SELECT
        id,
        metric,
        COUNT(DISTINCT IF(DATEDIFF(TIMESTAMP('{execution_date:s}'), TIMESTAMP(ds)) < 7, ds, NULL)) AS L7
    FROM
        [xxx:yyy.zzz]
    GROUP EACH BY
        id, metric
),
subq_L14 AS (
    SELECT
        id,
        metric,
        COUNT(DISTINCT IF(DATEDIFF(TIMESTAMP('{execution_date:s}'), TIMESTAMP(ds)) < 14, ds, NULL)) AS L14
    FROM
        [xxx:yyy.zzz]
    GROUP EACH BY
        id, metric
)
SELECT * FROM subq_L1
UNION ALL
SELECT * FROM subq_L7
UNION ALL
SELECT * FROM subq_L14

'{execution_date:s}'被python格式化为日期字符串。

BigQuery不断抛出此错误:

Encountered " "WITH" "WITH "" at line 1, column 1. Was expecting: <EOF>.

如果它们独立,所有查询都会成功运行;它只是将它们放在使其不成功的WITH子句中。

为什么这些没有成功?

1 个答案:

答案 0 :(得分:3)

WITH仅适用于标准SQL - 请参阅Enable Standard SQL

此外,在标准SQL中,您应该使用`xxx.yyy.zzz`而不是[xxx:yyy.zzz]

注意
也可以在不更改代码的情况下强制执行标准SQL 只需确保查询的第一行是

#StandardSQL

并且BigQuery会将查询的其余部分视为标准SQL