First Last Non Null Per Group
Get the first and last non null values per group using window functions with ignore nulls framing.
Code
SELECT DISTINCT
partition_key,
FIRST_VALUE(value) IGNORE NULLS OVER (
PARTITION BY partition_key
ORDER BY sort_key
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS first_non_null,
LAST_VALUE(value) IGNORE NULLS OVER (
PARTITION BY partition_key
ORDER BY sort_key
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS last_non_null
FROM observations
ORDER BY partition_key;Report a problem with this exampleOpens a pre-filled issue on GitHub.