I have problem in rolling over value from previous row when value is null. I currently have a dataset
+------+---------+ month. average +------+---------+ 01/2018 null 02/2018 null 03/2018 4 04/2018 10 05/2018 null 06/2018 20 07/2018 null My desired output should look similar to this. I'm trying to roll over previous value where average is null
+------+---------+ month. average +------+---------+ 01/2018 0 02/2018 0 03/2018 4 04/2018 10 05/2018 10 06/2018 20 07/2018 20 I tried the lag function but the null keeps appearing.
my current query looks similar to this:
with cte as( select to_char(created_at,'MM') || to char(created_at,'YYYY') as month ,AVG(CASE WHEN status ='ready' THEN col ELSE null END) as average from table group by month ) select month ,lag(average) over (order by month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as average from cte