1

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

无法识别的名称:total_users 在 [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

在此处输入图像描述

指针/提示是最受欢迎的。谢谢!

4

2 回答 2

4

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

#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')
于 2017-02-24T00:00:35.013 回答
0

这个如何?

SELECT a
FROM table_with_parameters AS t
JOIN my_table_fuction(t.par1, t.par2, t.par3) AS u
于 2022-02-24T22:26:57.423 回答