How do you count arrivals in the last five seconds of an irregular log?

With a numeric ordering key, an offset RANGE frame admits rows within a value distance, including the boundary.

SQLite example and interpretation

At second ten, arrivals at five and ten are both included. Convert timestamp text to consistent numeric units before applying a distance frame.

WITH r(t) AS (VALUES(1),(5),(10)) SELECT t,count(*) OVER(ORDER BY t RANGE BETWEEN 5 PRECEDING AND CURRENT ROW) FROM r ORDER BY t;

Result [[1, 1], [5, 2], [10, 2]]

中文

事件不均匀时怎样统计过去五秒的到达数?

数值 ORDER BY 配合 RANGE 偏移按排序值距离取窗口,边界上的事件也属于帧。

SQLite 示例与适用条件

时间 1、5、10 秒中,10 秒窗口含 5 和 10。若输入是时间文本,先换成统一单位的数值再使用距离。

WITH r(t) AS (VALUES(1),(5),(10)) SELECT t,count(*) OVER(ORDER BY t RANGE BETWEEN 5 PRECEDING AND CURRENT ROW) FROM r ORDER BY t;

结果 [[1, 1], [5, 2], [10, 2]]

References / 参考来源

Keep reading / 继续阅读