Why can LAST_VALUE return the current row?
LAST_VALUE reads the last row of the frame. With ORDER BY the default frame normally ends at the current peer group.
SQLite example and interpretation
To show final loss on every training step, extend the frame through UNBOUNDED FOLLOWING. A deterministic step order is still required.
WITH r(step,loss) AS (VALUES(1,8),(2,3)) SELECT step,last_value(loss) OVER(ORDER BY step ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM r ORDER BY step;Result [[1, 3], [2, 3]]
中文
为什么 LAST_VALUE 有时返回当前行?
LAST_VALUE 取窗口帧的最后一行。有 ORDER BY 时默认帧通常只到当前同值组的末尾。
SQLite 示例与适用条件
若两步训练都要显示最终损失,明确把帧延伸到 UNBOUNDED FOLLOWING。仍需用确定的步序定义何为最终。
WITH r(step,loss) AS (VALUES(1,8),(2,3)) SELECT step,last_value(loss) OVER(ORDER BY step ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM r ORDER BY step;结果 [[1, 3], [2, 3]]