返回在嵌套对象上过滤的BigQuery数据

时间:2014-12-18 19:44:05

标签: arrays nested google-bigquery

我正在尝试创建一个返回在2个嵌套对象上过滤的数据的查询。我已经在代码中添加了(1)和(2)来表示我想要来自两个不同嵌套对象的结果(我知道这不是一个有效的查询)。我一直在看录音,但我无法理解它。

SELECT externalIds.value(1) AS appName, externalIds.value(2) AS driverRef, SUM(quantity)/ 60 FROM [billing.tempBilling]
WHERE callTo = 'example' AND externalIds.type(1) = 'driverRef' AND externalIds.type(2) = 'applicationName'
GROUP BY appName, driverRef ORDER BY appName, driverRef;

加载到BigQuery中的数据如下所示:

{
  "callTo": "example",
  "quantity": 120,
  "externalIds": [
    {"type": "applicationName", "value": "Example App"},
    {"type": "driverRef", "value": 234}
  ]
}

我追求的结果是:

+-------------+-----------+----------+
|   appName   | driverRef | quantity |
+-------------+-----------+----------+
| Example App |    123    |   12.3   |
| Example App |    234    |  132.7   |
| Test App    |    142    |   14.1   |
| Test App    |    234    |   17.4   |
| Test App    |    347    |  327.5   |
+-------------+-----------+----------+

1 个答案:

答案 0 :(得分:1)

如果您需要求和的所有数量都在同一记录中,那么您可以使用WITHIN RECORD进行此查询。使用NTH()WITHIN RECORD获取记录中字段的第一个和第二个值。然后使用HAVING执行过滤,因为它需要一个由聚合函数计算的值。

SELECT callTo,
       NTH(1, externalIds.type) WITHIN RECORD AS firstType,
       NTH(1, externalIds.value) WITHIN RECORD AS maybeAppName,
       NTH(2, externalIds.type) WITHIN RECORD AS secondType,
       NTH(2, externalIds.value) WITHIN RECORD AS maybeDriverRef,
       SUM(quantity) WITHIN RECORD
FROM [billing.tempBilling]
HAVING callTo LIKE 'example%' AND
       firstType = 'applicationName' AND
       secondType = 'driverRef';

如果要汇总的数量分布在多个记录中,那么您可以从这种方法开始,然后按密钥分组,并在外部查询中对这些数量求和。