I do have the following three tables within postgres
ShipmentTrip
id,shipment_id, type,status,lat,long
1, 1, pickup, whatever, 25, 75
2, 1, dropoff, whatever, 27, 76
3, 2, pickup, whatever, 25, 75
4, 2, dropoff, whatever, 27, 76
Shipment
id,...,driver_id
Driver
id
I am trying to calculate the full distance a driver made
I tried different ways but I am not able to solve it
Tried cross join and subqueries and many different approaches but still no result
2
Answers
You have the PostGIS extension for PostgreSQL to calculate distances from latitude and longitude values.
You should use PostGIS functions for that purposes, here an example with your shipment_id 1:
Based on your sample, query should look like
Coordinates in lat long system of reference SRID (units in degrees) are transformed to a metric system (3857) in order to obtain a distance in meters.
If you don’t have PostGIS extension installed in your database
However it’s a straight linear distance between two points, not a distance following roads.
Docs for distance https://postgis.net/docs/ST_Distance.html and for points
https://postgis.net/docs/ST_MakePoint.html
Fiddle https://dbfiddle.uk/7XFHZWLA