on-internet
← Community

Fill null values with linear interpolation using DuckDB

Stack Overflow ·

I am trying to fill null values in a numeric table using duckdb. Let's consider the simple table df: x y 0 2.0 1 NULL 3 NULL 5 7.0 6 4.0 8 5.0 9 NULL that you can create with CREATE TABLE df (x NUMERIC, y NUMERIC) INSERT INTO df VALUES (0, 2), (1, NULL), (3, NULL), (5, 7), (6, 4), (8, 5), (9, NULL) then i would like to create a SELECT statement to retrieve: x y 0 2.0 1 3.0 3 5.0 5 7.0 6 4.0 8 5.0 9 NULL I think that a correlated subquery which finds the following and previous non-null values and then computes the interpolation should work: SELECT x, COALESCE( y, ( SELECT prev.y + ( (next.y - p

Comments

Sign in or sign up to comment and upvote.

No comments yet.