BigQuery WITH 子句抛出 "Unrecognized name" 错误

BigQuery WITH clause throws "Unrecognized name" error

我想使用 WITH 运算符从我生成的视图 (foo.bar.cohort) 中获取计数,然后可用于从我的主视图中的数据计算值 (foo.bar.retention_30d ).看起来这应该非常简单,但是当我尝试执行以下查询时出现此错误:

Unrecognized name: total_users at [12:29]

查询如下:

  #standardsql
WITH
  total_users AS (
  SELECT
    COUNT(DISTINCT uid) as sum_users
  FROM
    `foo.bar.cohort`
  WHERE
    first_seen_date > '2016-12-31'
    AND first_seen_date < '2017-02-01')
SELECT
  ROUND(SUM(retained_users)/total_users.sum_users, 2) AS avg,
  SUM(retained_users) AS retained,
  SUM(active_users) AS active,
  total_users.sum_users
FROM
  `foo.bar.retention_30d`
WHERE
  period = 27
  AND (created_at > '2016-12-31'
    AND created_at < '2017-02-01')

foo.bar.cohort 的架构:

foo.bar.retention_30d 的架构:

Pointers/hints 非常欢迎。谢谢!

total_users 是 table,不是列。如果要引用其中的列,则需要改用该名称。例如:

#standardsql
WITH
  total_users AS (
  SELECT
    COUNT(DISTINCT uid) as sum_users
  FROM
    `foo.bar.cohort`
  WHERE
    first_seen_date > '2016-12-31'
    AND first_seen_date < '2017-02-01')
SELECT
  ROUND(SUM(retained_users)/(SELECT sum_users FROM total_users), 2) AS avg,
  SUM(retained_users) AS retained,
  SUM(active_users) AS active,
  (SELECT sum_users FROM total_users) AS sum_users
FROM
  `foo.bar.retention_30d`
WHERE
  period = 27
  AND (created_at > '2016-12-31'
    AND created_at < '2017-02-01')

这个怎么样?

SELECT a
FROM table_with_parameters AS t
JOIN my_table_fuction(t.par1, t.par2, t.par3) AS u