Anda dapat menggunakan subkueri. Subquery akan mendapatkan Max(CompletedDate)
. Anda kemudian mengambil nilai ini dan bergabung di meja Anda lagi untuk mengambil catatan yang terkait dengan tanggal tersebut:
select ET1.TrainingID,
ET1.CompletedDate,
ET1.Notes
from HR_EmployeeTrainings ET1
inner join
(
select Max(CompletedDate) CompletedDate, TrainingID
from HR_EmployeeTrainings
--where AvantiRecID IS NULL OR AvantiRecID = @avantiRecID
group by TrainingID
) ET2
on ET1.TrainingID = ET2.TrainingID
and ET1.CompletedDate = ET2.CompletedDate
where ET1.AvantiRecID IS NULL OR ET1.AvantiRecID = @avantiRecID