【问题标题】:WHERE IN query - Is not supported by BigQuery or Resources exceededWHERE IN 查询 - BigQuery 不支持或超出资源
【发布时间】:2016-11-09 07:26:22
【问题描述】:

我有一张这样的桌子

header: source,  user, metric1, metric2,...
data:
source1, user1, metrics..
source1, user2, metrics..
source2, user1, metrics..
source3, user1, metrics...
source3, user3, metrics...

...

我想为每个来源都找不到的用户汇总一些指标。 在上面的示例中,我想为 source2 提取:users2 和 user3 并获取 avg 或它们的指标。

调查类似:

select avg(metric1) from tbl as tbl1
where user is not in 
  (select user from tbl as tbl2 where tbl1.source=tbl2.source)
group by source

根据文档页面,上述查询不适用于旧版 SQL: (https://cloud.google.com/bigquery/docs/reference/legacy-sql) 但在 ANSI 中有,但我得到了Resources exceeded

【问题讨论】:

  • 如果您发布工作 ID,Google 人员可以为您深入挖掘
  • 即使您为 source2 提供了预期的“逻辑”,我仍然看到很多方法来解释这一点!您也可以提供对 source1 和 source3 的期望吗?它可能会消除不确定性!
  • @MikhailBerlyant - 不知道你的意思。我正在尝试在一个名为 source 的字段上单独加入该表。 IE。我需要找到所有用户(user 字段的唯一值),而不是找到每个刚刚找到的用户的所有行,并在 metric1 字段上获取所有这些行的平均值>
  • 但您还提到了“......对于每个来源都没有找到的用户......”这让它有点模糊。所以这就是为什么我要求你为 source1 和 source3 提供预期的输出 - 这可以澄清你到底需要什么

标签: google-bigquery


【解决方案1】:

如果内部 SELECT 返回的记录数很大,通常不认为使用 WHERE IN / NOT IN 是一个好习惯。尝试使用 LEFT OUTER JOIN 重写您的查询,例如

select tbl1.source as source, avg(tbl1.metric1) as avg_metric1 from tbl as tbl1 left outer join tbl as tbl2 on tbl1.source = tbl2.source where tbl2.user is null group by source

【讨论】:

  • 非常感谢@Dmitri Safine。但是,该查询适用于我在 10k 秒后中断的 3000 万行,而没有等到它完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多