I'm on Redshift. Given the following data:
CREATE TABLE test (
id INT,
val1 INT,
val2 INT
);
INSERT INTO test VALUES
(1, 0, NULL),
(2, 0, NULL),
(3, 13, 1),
(4, 0, NULL),
(5, 0, NULL),
(6, 0, NULL),
(7, 0, NULL),
(8, 21, 2),
(9, 0, NULL),
(10, 143,3)
;
I would like to fill in the missing val2 values โโwith the first next non-zero value, for example.
INSERT INTO results VALUES
(1, 0, 1),
(2, 0, 1),
(3, 13, 1),
(4, 0, 2),
(5, 0, 2),
(6, 0, 2),
(7, 0, 2),
(8, 21, 2),
(9, 0, 3),
(10,143,3)
;
What is the best way to do this in Redshift / Postgres 8.0.2?