First Last Non Null Per Group

Get the first and last non null values per group using window functions with ignore nulls framing.

SqlData Fetching

Code

snippet
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;