update episode
set episode.DateOut = me.ExaminationDate
from akuz.T_MEDICAL_EPISODE episode
join akuz.T_MEDICAL_EXAMINATION_DATA med
	on med.MedicalEpisodeID = episode.MedicalEpisodeID
join akuz.T_MEDICAL_EXAMINATION me
	on me.MedicalRecordID = med.LastMedicalExamination
where episode.DateOut is null or episode.DateOut != me.ExaminationDate