试试这个:
merge into table01 a using ( select rownumber() over(partition by col1 order by col1_hist desc) rn_, t.* from table01 t where active=1 ) m on m.rn_=1 and a.active=1 and a.col1=m.col1 and a.col1_hist<>m.col1_hist when matched then update set active=0, valid_to=m.valid_from;
rn_ 内部子选择的列具有值 1 对于最高的每一行 col1_hist 在一组记录中具有相同的记录 col1 。 我们更新除了这些之外的所有其他活动行 valid_from 每组的价值。
rn_
1
col1_hist
col1
valid_from