Hello. I already asked the question: How to defragment the dates in the database? and a solution was proposed there, but as it turned out, there is an error in this decision.
This is how the base looks like initially. If you apply this code:
set @today:=date('2016-12-11'); update main M join ( select cid, nomer from ( select A.nomer, @cstart:=if(@cnum=A.nomer,@cstart,A.start), @cnum:=A.nomer, @cid:=(select M.id from main M,allnomer MT where MT.nomer=M.numbernomer and MT.type='lux' and M.datestart>@cstart and find_in_set(M.id,@used)=0 order by datestart limit 1 ) cid, @cstart:=(select dateend from main where id=@cid) dend, @used:=coalesce(concat(@used,',',@cid),@used) from ( select A.nomer, A.start from ( select A.nomer, (select coalesce(min(dateend),@today-interval 1 day) from main M where datestart<=@today and M.numbernomer=A.nomer) start from allnomer A where type='lux' ) A, main M, allnomer MT where MT.nomer=M.numbernomer and MT.type='lux' and M.datestart>A.start order by A.start desc, A.nomer ) A, (select @cid:=0,@cnum:=0,@cstart:=NULL,@used:='') Y ) X where cid is not null ) U on M.id=U.cid set M.numbernomer=U.nomer That base will look like this: (it should look like this after defragmentation) 
But if you change set @today: = date ('2016-12-11'); at set @today: = date ('2016-12-05'); (or any date younger than 10), then defragmentation will be like this: 
Tell me how to fix it?
datediff()function was used just to return the difference of two dates. And what he will throw where the actual sorting and decides. There now stands only by the start date, it is impossible to predict which record with the same start dates it will take - Mike