perpetualshaun;10928818 wrote:
There's got to be some way to group by the label name so that it only appears ONCE in my result set, and pulls the most recent timestamp and the appropriate status - tied to the most recent timestamp.
Going back to the original query you posted, try something along the lines of
SELECT sub.pkg_label, sub.max_pkg_timestamp, rtp.pkg_status
FROM (
SELECT pkg_label, max(pkg_timestamp) max_pkg_timestamp
FROM raco_trip_details AS p
LEFT JOIN customers AS c on c.customer_id = p.pkg_customer_id
LEFT JOIN labs AS l on l.lab_id = p.pkg_lab_id
WHERE 1
GROUP BY pkg_label
ORDER BY max_pkg_timestamp DESC, pkg_label ASC
) AS sub
INNER JOIN raco_trip_details AS rtp ON rtp.pkg_timestamp = sub.max_pkg_timestamp
The nested query does exactly the same thing as your first query did, except that it only pulls data that is either part of an aggregate function (pkg_timestamp) or part of the GROUP BY clause (pkg_label).
The outer query joins information from the nested query together with raco_trip_details (which I assumed is the one storing pkg_timestamp and pkg_status) on pkg_timestamp and the max_pkg_timestamp retrieved earlier. Now you only get the rows with the appropriate information, and as such you can also retrieve the pkg_status.
But if I understood your query correctly, take it one step further and
[code]
SELECT sub.pkg_label, sub.max_pkg_timestamp, rtp.pkg_status, c.customer_name, l.lab_name
FROM (
SELECT pkg_label, max(pkg_timestamp) max_pkg_timestamp
FROM raco_trip_details AS p
GROUP BY pkg_label
ORDER BY max_pkg_timestamp DESC, pkg_label ASC
) AS sub
INNER JOIN raco_trip_details AS rtp ON rtp.pkg_timestamp = sub.max_pkg_timestamp
LEFT JOIN customers AS c on c.customer_id = rtp.pkg_customer_id
LEFT JOIN labs AS l on l.lab_id = rtp.pkg_lab_id