By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE `table`(
`timestamp` timestamp,
`min` int
);
INSERT INTO `table` VALUES ('2018-10-03',2);
INSERT INTO `table` VALUES ('2018-10-04',null);
INSERT INTO `table` VALUES ('2018-10-04 12:00',null);
INSERT INTO `table` VALUES ('2018-10-05',5);
INSERT INTO `table` VALUES ('2018-10-06',null);
INSERT INTO `table` VALUES ('2018-10-07',1);
INSERT INTO `table` VALUES ('2018-10-08',null);
SELECT `timestamp`,
`min`,
CASE WHEN `min` IS NULL THEN MAX(`min`) OVER(PARTITION BY grp ORDER BY `timestamp`) END
FROM (
SELECT `timestamp`,
`min`,
SUM(CASE WHEN `min` IS NOT NULL THEN 1 ELSE 0 END) OVER(ORDER BY `timestamp`) grp
from `table`
) t1
timestamp | min | CASE WHEN `min` IS NULL THEN MAX(`min`) OVER(PARTITION BY grp ORDER BY `timestamp`) END |
---|---|---|
2018-10-03 00:00:00 | 2 | null |
2018-10-04 00:00:00 | null | 2 |
2018-10-04 12:00:00 | null | 2 |
2018-10-05 00:00:00 | 5 | null |
2018-10-06 00:00:00 | null | 5 |
2018-10-07 00:00:00 | 1 | null |
2018-10-08 00:00:00 | null | 1 |