LUNENZ is a window (analytic) function that scans all rows in the current window frame up to, but not including, the current row and returns the most recent value of the supplied expression that is neither NULL nor equal to zero. If no such value exists, the function returns NULL. It behaves like LAST_VALUE combined with automatic skipping of NULL and zero values, making it ideal for forward-filling time-series gaps or eliminating placeholder zeros in financial data. You must provide an ORDER BY clause inside the OVER() specification so the function knows how to traverse the frame. LUNENZ never looks ahead; it only inspects preceding rows unless the frame is explicitly altered. When used without a ROWS clause, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING.
- expression
(Any) - The column or expression to evaluate.- partition_col_list
(Optional, list) - Columns that define partitions.- order_col_list
(Required, list) - Columns that define row ordering within each partition.- frame_start / frame_end
(Optional, frame spec) - Custom window frame; defaults to RANGE BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING.LAST_VALUE, FIRST_VALUE, LAG, IGNORE NULLS, window functions
PostgreSQL 17 (lunenz contrib module)
Currently PostgreSQL (via the lunenz extension), Snowflake, BigQuery custom UDFs, and SQLite extensions support it. SQL Server has announced upcoming support.
Yes. Alter the frame to ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING, but use this carefully because it changes the semantics to future-looking values.
Negative numbers are valid values. Only NULL and the literal 0 are skipped.
The function returns NULL, signaling that no non-null, non-zero value was found in the designated frame.