Сopy rows from one table to another and replace duplicates in the process
merge production as target  
using raw as source  
on target.refno = source.refno
when matched and souce.date &gt; target.date 
     then update set target.date = souce.date, target.value= souce.value 
when not matched  
     then insert ...


merge