Filter results from distinct count
Perhaps, I'm missing something, but it sounds like to want the max(formidnb) for each NumCavalier in the time slice you're processing.  I haven't fully syntax checked this, but I think you could add the following to just above your semicolon

AND d.FormIdNb = 
(
select MAX(FormIdNb) 
FROM #tmp_DailyMissions
WHERE NumCavalier = d.NumCavalier and
DateHeure &gt;= t.StartTime
AND DateHeure &lt; t.EndTime
)