The problem I’m having
with dt as
(
select * from (
select 0 as po_id, 2 as part_id,‘B’ as record_type, 10 as rate, ‘20-May-2024’::date as ship_date union all
select 1 as po_id, 2 as part_id,‘A’ as record_type, 10 as rate, ‘20-May-2024’::date as ship_date union all
select 1 as po_id, 2 as part_id,‘B’ as record_type, 20 as rate, ‘21-May-2024’::date as ship_date union all
select 1 as po_id, 3 as part_id,‘A’ as record_type, 15 as rate, ‘22-May-2024’::date as ship_date union all
select 2 as po_id, 2 as part_id,‘A’ as record_type, 15 as rate, ‘23-May-2024’::date as ship_date union all
select 2 as po_id, 3 as part_id,‘C’ as record_type, 15 as rate, ‘24-May-2024’::date as ship_date union all
select 4 as po_id, 2 as part_id,‘B’ as record_type, 15 as rate, ‘25-May-2024’::date as ship_date
)
)
select *,
lag(PART_ID ) over (order by po_id, part_id ) as prv_PART_ID,
lag(PO_ID ) over (order by po_id, part_id ) as prv_PO_ID,
case when record_type = ‘A’ then ship_date else prv_ship_date end as v_A_ship_date,
lag(v_A_ship_date ) over (order by po_id, part_id ) as prv_ship_date
from dt;
The problem is for the first row, the column prv_ship_date doesnt exist and dbt/snowflake is returning an invalid identifier error. Is there any option in Jinja to default NULL if a column is missing ? (not column value).
so for first row - po=0, the ship_date should be null