sql - Filling null values with last not null one => huge number of columns -
i want copy rows source_table target_table while filling null values last not null one.
what have :
source_table : | b | c | parent | seq_number 1 | 2 | null | 1 | 1 null | null | 1 | 1 | 2 null | 3 | 2 | 1 | 3 dest_table empty
what want :
dest_table : | b | c | parent | seq_number 1 | 2 | null | 1 | 1 1 | 2 | 1 | 1 | 2 1 | 3 | 2 | 1 | 3
to achieve i'm dynamically generating following sql :
insert target_table (a, b, c) select coalesce(a, lag(a ignore nulls) on (partition parent order seq_number)) a, coalesce(b, lag(b ignore nulls) on (partition parent order seq_number)) b, coalesce(c, lag(c ignore nulls) on (partition parent order seq_number)) c, source_table;
everything works fine if source , target tables have small number of columns. in case have 400+ columns (yes bad legacy , cannot changed) , got following error :
ora-01467: sort key long
- first don't understand error. because i'm using many lag functions use "order by"/"partition by" ? replace coalesce(a, lag(a....)) coalesce(a,a) , error disappear.
- then, there workaround or way achieve same result ?
thx
just using pl/sql anonymous block:
declare x tgt_table%rowtype; --keep values last row begin r in (select * src_table order seq_number) loop x.a := nvl(r.a, x.a); ... x.z := nvl(r.z, x.z); insert tgt_table values x; end loop; end;
Comments
Post a Comment