SELECT
s.id,
st_linelocatepoint(s.line_geo, s.pickup) AS pickup_loc,
st_linelocatepoint(s.line_geo, s.drop_off) AS drop_loc
FROM (
SELECT
r.id,
r.trip_id,
ST_MakePoint(cast(r.pickup ->> 'lng' AS FLOAT), cast(r.pickup ->> 'lat' AS FLOAT)) AS pickup,
ST_MakePoint(cast(r.drop_off ->> 'lng' AS FLOAT), cast(r.drop_off ->> 'lat' AS FLOAT)) AS drop_off,
LineStringFromPoints(t.extra_fields -> 'line' -> 'waypoints') AS line_geo
FROM rides r
JOIN trips t ON r.trip_id = t.id
WHERE r.status = 'completed'
AND r.trip_id = 7192
ORDER BY r.id DESC
LIMIT 10
) s;