WITH LatestTrekmp AS (
    SELECT 
        tr.F042TrekmpID,
        ex.N_EXPERT,
        ROW_NUMBER() OVER (PARTITION BY ex.N_EXPERT ORDER BY tr.datebeg DESC) AS rn
    FROM ies.T_F042_TREKMP tr
    JOIN ies.T_F004_EXPERT ex ON tr.N_EXPERT = ex.N_EXPERT
)
UPDATE ies.T_MEDICAL_REQUEST_ITEM mri
SET F042Trekmp = subquery.F042TrekmpID
FROM LatestTrekmp subquery
WHERE mri.F042Trekmp IS NULL
  AND subquery.rn = 1
  AND subquery.N_EXPERT = mri.Expert;