create table #t(id int,fid int)
insert into #t select 1,18
union all select 2,17
union all select 3,19
union all select 4,17
union all select 5,18
union all select 6,19
union all select 7,17
select max(id) as id,fid from #t group by fid order by max(id)