Сортировать можно. Чуть-чуть упростить тоже можно:
select
log1.link_id,
log2.value - log1.value as diff,
log2.date,
log1.date
from log as log1 join log as log2 on log1.link_id = log2.link_id
join (select max(date) d, link_id
from log
group by link_id) as log3 on log1.link_id = log3.link_id
where
log2.date = log3.d and
log1.date = (select max(date)
from log as log4
where log4.link_id = log1.link_id and log4.date < log3.d)
order by diff;
Написано
Войдите на сайт
Чтобы задать вопрос и получить на него квалифицированный ответ.