Andra svaret:Du kan uppdatera kolumnen old_id med följande påstående!
Update #customer
SET oldid =
(Select TOP 1 c_old.id from #customer c_old
where c_old.enddate <= #customer.startdate
and c_old.cust_no = #customer.cust_no
and c_old.meter_no = #customer.meter_no
and c_old.enddate =
(
SELECT max(c.enddate) FROM #customer c
where c_old.cust_no = c.cust_no
and c_old.meter_no = c.meter_no
and #customer.startdate >= c.enddate
)
)
from #customer
go