G
G
geebv2017-12-16 01:47:01
PostgreSQL
geebv, 2017-12-16 01:47:01

How can I move the value from the previous row to the next row?

Source table
A | B
--+---
1 | 0
2 | 0
3 | 0
4 | 3 <<
5 | 0
6 | 4 <<
Should be turned into
A | B
--+---
1 | 3
2 | 3
3 | 3
4 | 3 <<
5 | 4
6 | 4 <<
Null values ​​fill with the last encountered non-zero value

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
XareH, 2017-12-25
@XareH

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,rez where tbl.a=rez.a-1
)
SELECT * FROM rez order by a

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question