As the value from the previous row to move to the next row?

The source table

A | B
--+---
1 | 0
2 | 0
3 | 0
4 | 3 <<
5 | 0
6 | 4 <<

Need to turn in

A | B
--+---
1 | 3
2 | 3
3 | 3
4 | 3 <<
5 | 4
6 | 4 <<

Zero values to fill the last encountered non-zero value
June 10th 19 at 14:44
1 answer
June 10th 19 at 14:46
with recursive tbl as (
SELECT * FROM (values
(1,0),(2,0),(3,0),(4,3),
(5,0),(6,0),(7,4),
(8,0),(9,0),(10,7))
AS t(a,b)
)
,rez as (SELECT * FROM tbl where a=10
UNION
SELECT tbl.a,case when tbl.b=0 then rez.b else tbl.b end FROM tbl,where tbl rez.a=rez.a-1
)
SELECT * FROM a order by rez

Find more questions by tags PostgreSQLRecursion