with cteDuplicates as (
  select *, row_number() over(partition by ID order by ModifiedDate desc) as DuplicateIndex
  from dbo.ConnectionHistory
)
delete from cteDuplicates where DuplicateIndex > 1;

if not exists (select * from sys.objects where object_name(object_id) = 'PK_ConnectionHistory')
    alter table dbo.ConnectionHistory add constraint [PK_ConnectionHistory] primary key (ID);