select sum(distance) from (select
ST_Distance_Sphere(
point(points.longtitude, points.latitude),
(select
point(next.longtitude, next.latitude)
from
points as next
where
next.date > points.date
and
next.date between '2022-07-01 00:00:00' and '2022-07-05 00:00:00'
order by date asc limit 1)) as distance
from points
where date between '2022-07-01 00:00:00' and '2022-07-05 00:00:00'
order by date asc) as t;