我注意到BQL不允许我在WHERE或HAVING子句中引用作用域聚合。例如:
% bq query 'SELECT fullName, COUNT(children.name) WITHIN RECORD as numChildren FROM [persons.person]'
+---------------+-------------+
| fullName | numChildren |
+---------------+-------------+
| John Doe | 2 |
| Mike Jones | 3 |
| Anna Karenina | 0 |
+---------------+-------------+
% bq query 'SELECT fullName, COUNT(children.name) WITHIN RECORD as numChildren FROM [persons.person] WHERE numChildren > 0'
BigQuery error in query operation: Field 'numChildren' not found in table 'persons.person'.
% bq query 'SELECT fullName, COUNT(children.name) WITHIN RECORD as numChildren FROM [persons.person] HAVING numChildren > 0'
BigQuery error in query operation: Invalid use of HAVING clause. HAVING should be used only when non scoped aggregations are present. Non aggregating queries should use WHERE instead
我理解在WHERE子句中不允许组级聚合的基本原理,以及在WHERE或HAVING子句中不允许表级聚合的基本原理,但我想不出任何原因-aggregates不能出现在WHERE或HAVING子句中。任何人都可以提供理由吗?
答案 0 :(得分:2)
看起来您应该能够在HAVING子句中执行此操作。这已添加到我们可能的BigQuery SQL语言改进的内部文档中。
答案 1 :(得分:0)
好消息:现在可以了。
SELECT created_at, COUNT(payload.shas.actor_email) WITHIN RECORD AS xx
FROM [publicdata:samples.github_nested]
HAVING xx>1
LIMIT 10