在每个当前行和上一行 BigQuery 之间查找 MAX、AVG
Posted
技术标签:
【中文标题】在每个当前行和上一行 BigQuery 之间查找 MAX、AVG【英文标题】:Find MAX, AVG between every current and previous row BigQuery 【发布时间】:2020-12-01 07:06:55 【问题描述】:我有一个包含 150.000 行的表,其中包含 DateTime 和 Speed 列。行之间的时间戳差异为 10 秒。我想为每 20 秒段(2x 10 秒)计算 Speed 列的 MAX 和 AVG,所以基本上将每个当前行与其前一行进行比较并计算 Speed 列的 MAX 和 AVG。
预期结果:
DateTime Speed MAXspeed AVGspeed
2019-03-21 10:58:34 UTC 52
2019-03-21 10:58:44 UTC 50 52 51
2019-03-21 10:58:54 UTC 55 55 52.5
2019-03-21 10:59:04 UTC 60 60 57.5
2019-03-21 10:59:14 UTC 65 65 62.5
2019-03-21 10:59:24 UTC 63 65 64
2019-03-21 10:59:34 UTC 50 63 56.5
2019-03-21 10:59:44 UTC 50 50 50
2019-03-21 10:59:54 UTC 50 50 50
...
我尝试了下面的查询,但显然是错误的:
select *,
MAX(SpeedGearbox_km_h, LAG(SpeedGearbox_km_h) over (order by DateTime)) as Maxspeeg,
AVG(SpeedGearbox_km_h, LAG(SpeedGearbox_km_h) over (order by DateTime)) as AVGspeed,
from `xx.yy`
group by 1,2
order by DateTime
【问题讨论】:
【参考方案1】:只需在查询中使用ROWS BETWEEN 1 PRECEDING AND CURRENT ROW:
SELECT *,
MAX(SpeedGearbox_km_h) OVER (ORDER BY DateTime ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) as MAXspeed,
AVG(SpeedGearbox_km_h) OVER (ORDER BY DateTime ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) as AVGspeed
FROM `xx.yy`
ORDER BY DateTime
【讨论】:
感谢您的帮助!我收到此错误消息:ORDER BY 键必须是基于 RANGE 的窗口中的数字,具有 OFFSET PRECEDING 或 OFFSET FOLLOWING 边界,但在 [2:32] 处具有 TIMESTAMP 类型 我明白了。我需要将 RANGE 更改为 ROWS,它起作用了! 抱歉,已修复。 :)以上是关于在每个当前行和上一行 BigQuery 之间查找 MAX、AVG的主要内容,如果未能解决你的问题,请参考以下文章