Q&A for anime and manga fans
declare @Ordre table ( ordre_id int, code_dest char(1) ) declare @Voit table ( id_voit int, ordre_id int, code_dest char(1) ) insert into @Ordre values (1,'a'), (2,'b'), (3,'c'), (4,'a'), (5,'a'), (6,'b'), (7,'g') insert into @Voit select id_voit, ordre_id, rsOrdre.code_dest from @Ordre rsOrdre inner join ( select distinct code_dest, ROW_NUMBER() over (order by code_dest) as id_voit from @Ordre group by code_dest ) rsVoit on rsVoit.code_dest = rsOrdre.code_dest order by id_voit, ordre_id select * from @Voit