create table source_table (
header_id number, line_id number, account_type varchar2(30), accrual_tier varchar2(30)
)
Table created.
insert into source_table values (101 , 1 ,'expense' ,NULL)
1 row(s) inserted.
insert into source_table values (101 , 2 ,'liability' ,'TAX')
1 row(s) inserted.
insert into source_table values (102 , 1 ,'liability' ,'C'||chr(38)||'B')
1 row(s) inserted.
insert into source_table values (102 , 2 ,'expense' ,NULL)
1 row(s) inserted.
insert into source_table values (102 , 3 ,'expense' ,NULL)
1 row(s) inserted.
select header_id
, line_id
, account_type
, accrual_tier
, max(case when account_type = 'liability' then accrual_tier end) over (partition by header_id) as sub_accrual
from source_table
order by header_id
, line_id
HEADER_ID | LINE_ID | ACCOUNT_TYPE | ACCRUAL_TIER | SUB_ACCRUAL | 101 | 1 | expense | - | TAX | 101 | 2 | liability | TAX | TAX | 102 | 1 | liability | C&B | C&B | 102 | 2 | expense | - | C&B | 102 | 3 | expense | - | C&B |
---|
select header_id
, line_id
, account_type
, accrual_tier
, first_value (accrual_tier ignore nulls) over (partition by header_id order by account_type desc) as sub_accrual
from source_table
order by header_id
, line_id
/