This type of question comes up quite often in oracle forum.
Below is the test script
create table test(col_1 number, col_2 number,col_3 number);
insert into test values (1,1,4);
insert into test values (1,2,null);
insert into test values (1,3,null);
insert into test values (1,4,null);
insert into test values (1,5,5);
insert into test values (1,6,null);
insert into test values (2,1,2);
insert into test values (2,2,null);
insert into test values (2,3,7);
insert into test values (2,4,null);
insert into test values (3,5,null);
insert into test values (3,6,null);
insert into test values (4,1,null);
insert into test values (4,2,null);
insert into test values (4,3,null);
The data looks like this
SQL> select * from test
2 /
COL_1 COL_2 COL_3
---------- ---------- ----------
1 1 4
1 2
1 3
1 4
1 5 5
1 6
2 1 2
2 2
2 3 7
2 4
3 5
COL_1 COL_2 COL_3
---------- ---------- ----------
3 6
4 1
4 2
4 3
and the expected output is, i have done with MODEL but looking for a solution without that which is compatible in 9i .
SQL> select col_1, col_2, col_3
2 from (select row_number() over(order by col_1, col_2) rno,
3 col_1, col_2, col_3
4 from test)
5 model
6 return updated rows
7 dimension by (rno)
8 measures(col_1,col_2,col_3)
9 rules update
10 (
11 col_3[any]= case when nvl(col_3[cv()],0) = 0 then nvl(col_3[cv()-1],0) else col_3[cv()] end
12 )
13 /
COL_1 COL_2 COL_3
---------- ---------- ----------
1 1 4
1 2 4
1 3 4
1 4 4
1 5 5
1 6 5
2 1 2
2 2 2
2 3 7
2 4 7
3 5 7
COL_1 COL_2 COL_3
---------- ---------- ----------
3 6 7
4 1 7
4 2 7
4 3 7
15 rows selected.
that is replace the null values in col_3 with the previous not null value order by col_1 and col_2
Thanks,
Karthick.
ops$tkyte%ORA10GR2> select col_1, col_2, col_3, <b>last_value(col_3 ignore nulls) over (order by col_1, col_2) lv</b>
2 from test
3 order by col_1, col_2
4 /
COL_1 COL_2 COL_3 LV
---------- ---------- ---------- ----------
1 1 4 4
1 2 4
1 3 4
1 4 4
1 5 5 5
1 6 5
2 1 2 2
2 2 2
2 3 7 7
2 4 7
3 5 7
3 6 7
4 1 7
4 2 7
4 3 7
15 rows selected.