v.19.14New Feature

New Function Neighbor(value, Offset[, Default_value])

New function neighbor(value, offset[, default_value]). Allows to reach prev/next value within column in a block of data. #5925 (Alex Krash) 6685365ab8c5b74f9650492c88a012596eb1b0c6 341e2e4587a18065c2da1ca888c73389f48ce36c Alexey Milovidov
Introduces the neighbor(value, offset[, default_value]) function to access previous or next values within a column in a data block.

Why it matters

This feature provides an easy way to retrieve adjacent values in a column without complex window functions or self-joins, enabling efficient data comparisons and calculations that depend on neighboring rows.

How to use it

Use the neighbor function by specifying the target column as value, an offset integer to indicate how many rows ahead (positive) or behind (negative) to peek, and optionally a default_value to return if the neighbor does not exist. For example:

SELECT value, neighbor(value, 1) AS next_value, neighbor(value, -1, 0) AS prev_value FROM table