Может кому пригодится, удалось реализовать мою задачу через функцию:
DELIMITER $$
CREATE DEFINER=`root`@`localhost` FUNCTION `FN_GET_DISTANCE`(
lat1 DOUBLE, lng1 DOUBLE, lat2 DOUBLE, lng2 DOUBLE
) RETURNS double
BEGIN
DECLARE radlat1 DOUBLE;
DECLARE radlat2 DOUBLE;
DECLARE theta DOUBLE;
DECLARE radtheta DOUBLE;
DECLARE dist DOUBLE;
SET radlat1 = PI() * lat1 / 180;
SET radlat2 = PI() * lat2 / 180;
SET theta = lng1 - lng2;
SET radtheta = PI() * theta / 180;
SET dist = sin(radlat1) * sin(radlat2) + cos(radlat1) * cos(radlat2) * cos(radtheta);
SET dist = acos(dist);
SET dist = dist * 180 / PI();
SET dist = dist * 60 * 1.1515;
SET dist = dist * 1.609344;
RETURN dist;
END$$
DELIMITER ;
Далее вызываем эту функцию и делаем суммирование пройденного пути:
SELECT (SUM(CASE WHEN (FN_GET_DISTANCE(FIRST.Latitude,FIRST.Longitude,second.Latitude,second.Longitude))BETWEEN 0.01 AND 2 THEN FN_GET_DISTANCE(FIRST.Latitude,FIRST.Longitude,second.Latitude,second.Longitude) ELSE 0 END)) AS distance
FROM `data` AS first
JOIN `data` AS second
WHERE first.id +1 = second.id
and
first.ID_Dev = 1
and
first.DateTime BETWEEN '2021-05-23 08:00:00' AND '2021-05-23 20:00:00'
ORDER BY first.id ASC
Или второй вариант, мне больше подошел:
SELECT SUM(t.distance) as Distance FROM
(SELECT (CASE WHEN (FN_GET_DISTANCE(Latitude,Longitude,@OLDLatitude,@OLDLongitude)) BETWEEN 0.01 AND 2 THEN
FN_GET_DISTANCE(Latitude,Longitude,@OLDLatitude,@OLDLongitude) ELSE 0 END) AS distance,
IF(@OLDLatitude IS NOT NULL, @OLDLatitude := Latitude, 0),
IF(@OLDLongitude IS NOT NULL, @OLDLongitude := Longitude, 0)
FROM `data` , (SELECT @OLDLatitude := 0) var0, (SELECT @OLDLongitude := 0) var1
WHERE ID_Dev = 1
AND DateTime BETWEEN '2021-05-23 08:00:00' AND '2021-05-23 20:00:00'
ORDER BY ID DESC) t;
Написано
Войдите на сайт
Чтобы задать вопрос и получить на него квалифицированный ответ.
DELIMITER $$
CREATE DEFINER=`root`@`localhost` FUNCTION `FN_GET_DISTANCE`(
lat1 DOUBLE, lng1 DOUBLE, lat2 DOUBLE, lng2 DOUBLE
) RETURNS double
BEGIN
DECLARE radlat1 DOUBLE;
DECLARE radlat2 DOUBLE;
DECLARE theta DOUBLE;
DECLARE radtheta DOUBLE;
DECLARE dist DOUBLE;
SET radlat1 = PI() * lat1 / 180;
SET radlat2 = PI() * lat2 / 180;
SET theta = lng1 - lng2;
SET radtheta = PI() * theta / 180;
SET dist = sin(radlat1) * sin(radlat2) + cos(radlat1) * cos(radlat2) * cos(radtheta);
SET dist = acos(dist);
SET dist = dist * 180 / PI();
SET dist = dist * 60 * 1.1515;
SET dist = dist * 1.609344;
RETURN dist;
END$$
DELIMITER ;
Далее вызываем эту функцию и делаем суммирование пройденного пути:
SELECT (SUM(CASE WHEN (FN_GET_DISTANCE(FIRST.Latitude,FIRST.Longitude,second.Latitude,second.Longitude))BETWEEN 0.01 AND 2 THEN FN_GET_DISTANCE(FIRST.Latitude,FIRST.Longitude,second.Latitude,second.Longitude) ELSE 0 END)) AS distance
FROM `data` AS first
JOIN `data` AS second
WHERE first.id +1 = second.id
and
first.ID_Dev = 1
and
first.DateTime BETWEEN '2021-05-23 08:00:00' AND '2021-05-23 20:00:00'
ORDER BY first.id ASC
Или второй вариант, мне больше подошел:
SELECT SUM(t.distance) as Distance FROM
(SELECT (CASE WHEN (FN_GET_DISTANCE(Latitude,Longitude,@OLDLatitude,@OLDLongitude)) BETWEEN 0.01 AND 2 THEN
FN_GET_DISTANCE(Latitude,Longitude,@OLDLatitude,@OLDLongitude) ELSE 0 END) AS distance,
IF(@OLDLatitude IS NOT NULL, @OLDLatitude := Latitude, 0),
IF(@OLDLongitude IS NOT NULL, @OLDLongitude := Longitude, 0)
FROM `data` , (SELECT @OLDLatitude := 0) var0, (SELECT @OLDLongitude := 0) var1
WHERE ID_Dev = 1
AND DateTime BETWEEN '2021-05-23 08:00:00' AND '2021-05-23 20:00:00'
ORDER BY ID DESC) t;