Una forma de hacerlo es con funciones de clasificación anidadas. Primero, asigne un valor constante a todo lo que obtenga un valor (usando max() over
) y luego use esto como una partición.
select t.Invoice_Id, t.Invoice_Line, t.Kit_Flag, t.Part_Number,
max(KitPart) over (partition by invoice_id, KitNum) as Parent_Part
from (select t.*,
sum(isKit) over (partition by InvoiceId order by InvoiceLine) as KitNum
from (select t.Invoice_Id, t.Invoice_Line, t.Kit_Flag, t.Part_Number,
(case when Kit_Flag = 'K' then 1 else 0 end) as IsKit,
(case when Kit_Flag = 'K' then Part_Number end) as KitPart
from Invoice_Data t
) t
) t