时间序列数据的大查询

bigquery for time series data

我有一个 table 这样的。该行包括时间戳和计数作为当时值的度量。

Row timestamp count
1 2018-08-20 04:01:39.108497 31
2 2018-08-20 04:01:45.109497 45
3 2018-08-20 04:01:49.109497 44
4 2018-08-20 04:02:39.102497 33
5 2018-08-20 04:02:45.101497 41
6 2018-08-20 04:02:49.103497 22
7 2018-08-20 04:03:39.102497 23
8 2018-08-20 04:03:45.102497 42
9 2018-08-20 04:03:49.103497 41

我想将其汇总为分钟级别的汇总,如 avg(count)

Row timestamp count
1 2018-08-20 04:01:00 40
2 2018-08-20 04:02:00 32
3 2018-08-20 04:03:00 35

请帮忙。提前致谢

只需使用TIMESTAMP_TRUNC():

select timestamp_trunc(minute, timestamp) as timestamp_min,
       sum(count)  -- or whatever aggregation you want
from t
group by timestamp_min;

你的问题不清楚你想要什么聚合。例如,“35”没有出现在数据中。

以下适用于 BigQuery 标准 SQL

#standardSQL
SELECT TIMESTAMP_TRUNC(ts, MINUTE) dt, CAST(AVG(cnt) AS INT64) viewCount
FROM `project.dataset.table`
GROUP BY dt

如果按以下方式应用于您问题中的虚拟数据

#standardSQL
WITH `project.dataset.table` AS (
  SELECT TIMESTAMP '2018-08-20 04:01:39.108497' ts, 31 cnt UNION ALL
  SELECT '2018-08-20 04:01:45.109497', 45 UNION ALL
  SELECT '2018-08-20 04:01:49.109497', 44 UNION ALL
  SELECT '2018-08-20 04:02:39.102497', 33 UNION ALL
  SELECT '2018-08-20 04:02:45.101497', 41 UNION ALL
  SELECT '2018-08-20 04:02:49.103497', 22 UNION ALL
  SELECT '2018-08-20 04:03:39.102497', 23 UNION ALL
  SELECT '2018-08-20 04:03:45.102497', 42 UNION ALL
  SELECT '2018-08-20 04:03:49.103497', 41 
)
SELECT TIMESTAMP_TRUNC(ts, MINUTE) dt, CAST(AVG(cnt) AS INT64) viewCount
FROM `project.dataset.table`
GROUP BY dt
-- ORDER BY dt

结果是

Row dt                      viewCount
1   2018-08-20 04:01:00 UTC 40   
2   2018-08-20 04:02:00 UTC 32   
3   2018-08-20 04:03:00 UTC 35