germancin
12/10/2018 - 7:13 AM

group by a table and then sort it.

group by a table and then sort it.

SELECT sa.*, s.*
FROM shipment_activities sa
JOIN shipments s ON s.id = sa.shipment_id
INNER JOIN
(
    SELECT max(date_time) maxTime, shipment_id, status_description
    FROM shipment_activities 
    GROUP BY shipment_id
) sa2
  ON sa.shipment_id = sa2.shipment_id
  WHERE sa.date_time = sa2.maxTime AND
  sa.status_description != 'Delivered'
  GROUP BY sa.shipment_id
  ORDER BY sa.shipment_id ASC