How can a log show a mean over its last three observations?

ROWS positions frame boundaries by row count. Two preceding through the current row covers up to three observations.

SQLite example and interpretation

For readings two, four and nine the third average is five. Early rows have shorter frames, and irregular sampling changes the elapsed time covered.

WITH r(t,v) AS (VALUES(1,2),(2,4),(9,9)) SELECT t,avg(v) OVER(ORDER BY t ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) FROM r ORDER BY t;

Result [[1, 2.0], [2, 3.0], [9, 5.0]]

中文

怎样在日志表中计算最近三条记录的均值?

ROWS 按排序后的行定位帧边界,2 PRECEDING 到 CURRENT ROW 最多覆盖三项。

SQLite 示例与适用条件

读数 2、4、9 的第三行均值为 5。首行只有一个可用值,采样不均时三条记录覆盖的时间跨度也会变化。

WITH r(t,v) AS (VALUES(1,2),(2,4),(9,9)) SELECT t,avg(v) OVER(ORDER BY t ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) FROM r ORDER BY t;

结果 [[1, 2.0], [2, 3.0], [9, 5.0]]

References / 参考来源

Keep reading / 继续阅读