I have the following table with some sample data.
Record_ID Counter Serial Owner
1 0 AAA Jack
2 1 AAA Kevin
3 0 BBB Jane
4 1 BBB Wendy
Based on data similar to the above, I am trying to write a SQL query for MySQL that gets the record with the maximum Counter
value per Serial
number. The part I seem to be having trouble with is getting the query to get the last 50 unique serial numbers that were updated.
Below is the query I came up with so far based on this StackOverflow question.
SELECT *
FROM `history` his
INNER JOIN(SELECT serial,
Max(counter) AS MaxCount
FROM `tracking`
WHERE serial IN (SELECT serial
FROM `history`)
GROUP BY serial
ORDER BY record_id DESC) q
ON his.serial = q.serial
AND his.counter = q.maxcount
LIMIT 0, 50