>;with cteInvoiceDetails as (select *, row_number() over (partition by InvoiceNo order by InvoiceDate DESC) as Rn) >select * from cte c1 where Rn = 1 and NoStatus = 7 and >exists (select 1 from cte c2 where c2.InvoiceNo = c1.InvoiceNo and c2.Rn = 2 and NoStatus = 3) -- so, the previous row has status 3 and therefore this row may be deleted >Interesting approach, thanks