How do I retrieve, using MySQL, one record per document id with the maximum revision? For example:
SELECT doc_key, MAX(ver) AS top_ver
FROM docs
GROUP BY doc_key;
How do I retrieve, using MySQL, one record per document id with the maximum revision? For example:
SELECT doc_key, MAX(ver) AS top_ver
FROM docs
GROUP BY doc_key;
Using a join with a subquery is a method that has worked reliably in my experience. In this approach, you first generate a temporary table containing each document id and its maximum revision. Then, you join back on the main table to retrieve additional details if necessary. This method not only ensures clarity in your query structure but also offers flexibility if further data needs to be fetched for each record. This approach is particularly useful when dealing with extensive datasets where optimization is essential.