update IES.T_F001_TFOMS fm
set D_END = current_timestamp
where fm.D_END is null
        and
    exists
    (
        select *
        from IES.T_F001_TFOMS fd
        where fd.TF_KOD <> fm.TF_KOD
                and
            fd.TF_OKATO = fm.TF_OKATO
                and
            fd.D_END is null
                and
            fd.D_EDIT > fm.D_EDIT
    )
