As part of my attribution modeling setup, I need to assign order_id's to the interactions that happened before the assigned transaction but after any previous order.
Example, I have a table like this:
And I would like to have somethin like this:
thus filling in where the order_id is null.
I have tried using a self join and calling the order_id where time is <= than the time in the second join but with no luck, this duplicates some of the rows.
EDIT:
here is my sql attempt:
select
c.cookie_id,
c.order_id,
c.channel,
c.min_report_timestamp
,case when lead(c.cookie_id) over (order by min_report_timestamp desc) is null then c.order_id else c.order_id end as order_id_ok
,lead(c.order_id) over (order by min_report_timestamp desc) as lead_order
--,lag(c.order_id) over (order by min_report_timestamp asc) as lag_order
from table c
group by 1,2,3,4
I have an idea on the conditions I should use, the problem is I can't make it fill in the missing spaces with the order_id i need, it doesn't "carry" the order id row by row

