SELECT
s.package_weight
,so.waypoint_zip AS origin_zip
,sd.waypoint_zip AS destination_zip
FROM
shipments s
INNER
JOIN
(SELECT
w.shipment_id
,MAX(w.waypoint_id) AS shipment_origin
,MIN(w.waypoint_id) AS shipment_destination
FROM
waypoints w
GROUP
BY
w.shipement_id) AS w
ON
s.shipment_id = w.shipment_id
INNER
JOIN
waypoints so
ON
s.shipment_id = so.shipment_id
AND
w.shipment_origin = so.waypoint_id
INNER
JOIN
waypoints sd
ON
s.shipment_id = sd.shipment_id
AND
w.shipment_destination = sd.waypoint_id
Bookmarks