By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
--
--
--
create table sample (
id int primary key,
flag tinyint,
coluna int
);
insert into
sample
values
(1, 0, 20)
,(2, 0, 23)
,(3, 0, 40)
,(4, 1, 45)
,(5, 1, 50);
select
id,
coluna,
lag(coluna) over (order by id) as lag_coluna
from sample
--
--
--
id | coluna | lag_coluna |
---|---|---|
1 | 20 | null |
2 | 23 | 20 |
3 | 40 | 23 |
4 | 45 | 40 |
5 | 50 | 45 |