clear markdown compare help best fiddles feedback dbanow.uk
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2805490 fiddles created (40770 in the last week).

create table tbl ( id bigint primary key, a int );
 hidden batch(es)


insert into tbl (id, a) select s, case when random() < .5 then s end from generate_series(0, 100) s; select * from tbl;
101 rows affected
id a
0
1 1
2
3
4 4
5 5
6
7
8
9 9
10
11 11
12
13 13
14 14
15
16 16
17
18
19
20
21 21
22 22
23
24 24
25 25
26
27
28 28
29
30 30
31 31
32 32
33 33
34
35
36
37 37
38
39
40
41
42
43
44 44
45 45
46 46
47
48 48
49 49
50
51 51
52
53 53
54 54
55 55
56
57
58 58
59 59
60 60
61 61
62 62
63 63
64 64
65 65
66 66
67 67
68 68
69
70
71 71
72
73 73
74 74
75
76
77 77
78
79
80
81 81
82 82
83
84 84
85
86 86
87 87
88
89 89
90 90
91
92
93
94 94
95
96 96
97
98
99
100
 hidden batch(es)


create function coalesce_r_sfunc(state anyelement, value anyelement) returns anyelement immutable parallel safe as $$ select coalesce(value, state); $$ language sql;
 hidden batch(es)


create aggregate find_last_ignore_nulls(anyelement) ( sfunc = coalesce_r_sfunc, stype = anyelement );
 hidden batch(es)


select *, ( select a from tbl it where it.id <= t.id and it.a is not null order by id desc limit 1 ) lnn from tbl t;
id a lnn
0
1 1 1
2 1
3 1
4 4 4
5 5 5
6 5
7 5
8 5
9 9 9
10 9
11 11 11
12 11
13 13 13
14 14 14
15 14
16 16 16
17 16
18 16
19 16
20 16
21 21 21
22 22 22
23 22
24 24 24
25 25 25
26 25
27 25
28 28 28
29 28
30 30 30
31 31 31
32 32 32
33 33 33
34 33
35 33
36 33
37 37 37
38 37
39 37
40 37
41 37
42 37
43 37
44 44 44
45 45 45
46 46 46
47 46
48 48 48
49 49 49
50 49
51 51 51
52 51
53 53 53
54 54 54
55 55 55
56 55
57 55
58 58 58
59 59 59
60 60 60
61 61 61
62 62 62
63 63 63
64 64 64
65 65 65
66 66 66
67 67 67
68 68 68
69 68
70 68
71 71 71
72 71
73 73 73
74 74 74
75 74
76 74
77 77 77
78 77
79 77
80 77
81 81 81
82 82 82
83 82
84 84 84
85 84
86 86 86
87 87 87
88 87
89 89 89
90 90 90
91 90
92 90
93 90
94 94 94
95 94
96 96 96
97 96
98 96
99 96
100 96
 hidden batch(es)


select *, find_last_ignore_nulls(a) over (order by id) from tbl;
id a find_last_ignore_nulls
0
1 1 1
2 1
3 1
4 4 4
5 5 5
6 5
7 5
8 5
9 9 9
10 9
11 11 11
12 11
13 13 13
14 14 14
15 14
16 16 16
17 16
18 16
19 16
20 16
21 21 21
22 22 22
23 22
24 24 24
25 25 25
26 25
27 25
28 28 28
29 28
30 30 30
31 31 31
32 32 32
33 33 33
34 33
35 33
36 33
37 37 37
38 37
39 37
40 37
41 37
42 37
43 37
44 44 44
45 45 45
46 46 46
47 46
48 48 48
49 49 49
50 49
51 51 51
52 51
53 53 53
54 54 54
55 55 55
56 55
57 55
58 58 58
59 59 59
60 60 60
61 61 61
62 62 62
63 63 63
64 64 64
65 65 65
66 66 66
67 67 67
68 68 68
69 68
70 68
71 71 71
72 71
73 73 73
74 74 74
75 74
76 74
77 77 77
78 77
79 77
80 77
81 81 81
82 82 82
83 82
84 84 84
85 84
86 86 86
87 87 87
88 87
89 89 89
90 90 90
91 90
92 90
93 90
94 94 94
95 94
96 96 96
97 96
98 96
99 96
100 96
 hidden batch(es)