Problem Statement
FIRST_VALUE and LAST_VALUE often need an explicit frame. Why?
Explanation
The default frame for ordered windows is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW in many engines. With peers, that frame can include more rows than expected, and LAST_VALUE may not reflect the true last row in the partition.
Set ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to get the absolute first or last within the partition, or use CURRENT ROW for stepwise behavior.
Code Solution
SolutionRead Only
LAST_VALUE(amount) OVER ( PARTITION BY product ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS last_amt
