Home > Mobile >  Difficulties trying to replace null values with values from previous row in snowflake using sql
Difficulties trying to replace null values with values from previous row in snowflake using sql

Time:02-15

Please I have a table, with null values and I am trying to write an sql query to replace them with values from the previous row

table: customer

id value
001 34
002 Null
003 Null
004 Null
005 20
006 Null
007 Null
008 55

what I want to achieve

id value
001 34
002 20
003 20
004 20
005 20
006 55
007 55
008 55

thanks

CodePudding user response:

Using enter image description here

CodePudding user response:

A subquery to get the values from the immediate following row where value is not null should do the job:

select id,
(select value from customer where id >= c.id and value is not null
order by id limit 1) as value
from customer c
order by id
  • Related