我被要求用 JavaScript 开发一个客户端应用程序,用于查询 BigQuery 上存储的一些表,因此目前我正在掌握查询语法。
我的架构具有以下字段:
download_speed: FLOAT
real_address: STRING
timestamp: INTEGER
upload_speed: FLOAT
[...]
我想找到不同日期汇总的平均上传和下载速度,并跟踪不同的 IP,所以我编写了以下查询:
SELECT LEFT( FORMAT_UTC_USEC(UTC_USEC_TO_DAY(timestamp*1000000)),10) as date_day,
AVG (download_speed)avg_download,
AVG (upload_speed)avg_upload,
real_address
FROM [xxxxxxxxxxxx:xxxxxxx.xxxx]
GROUP BY date_day, real_address
ORDER BY date_day ASC
这没关系,但结果是平坦的,我需要每天汇总平均速度,以便我可以将它们绘制在时间轴上。一种可能是处理查询客户端返回的 json,但对于大量数据集和许多具有不同地址的不同用户(正如我们应该拥有的那样),这种方法对我来说听起来是自杀行为。
所以我问自己是否有一种方法可以在字段中保存包含给定日期的所有地址的数组。我想要返回这个元组结构:
+---------------+-------------------+--------------------+--------------------+
| date_day | avg_download | avg_upload | real_addr_list |
+---------------+-------------------+--------------------+--------------------+
| | | | 79.20.xx.xx, |
| 2013-01-13 | 510574.63 | 29599.92 | 151.46.xxx.xx, |
| | | | 151.70.xx.xx |
+---------------+-------------------+--------------------+--------------------+
这可能吗,还是我被迫在 BigQuery 之外处理结果?
预先感谢您的帮助。
最佳答案
好的,我发现 BigQuery 支持函数 GROUP_CONCAT('str'),其用法如下:
Concatenates multiple strings into a single comma-delimited string, rather like SUM() for strings. Use this with a grouping statement and a field name for str to concatenate a list of all string values in a group into a single string
如果有人感兴趣,查询是:
SELECT LEFT( FORMAT_UTC_USEC(UTC_USEC_TO_DAY(timestamp*1000000)),10) as date_day,
AVG (download_speed)avg_download,
AVG (upload_speed)avg_upload,
GROUP_CONCAT(real_address)as real_address_list
FROM [xxxxxxx:xxxxx.xxxxx]
GROUP BY date_day
ORDER BY date_day ASC
希望这对其他人也有用。
关于database - Bigquery : array of values in a field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14857207/