I have a MySQL table (called formstatus) with 4 columns: id, form, status, and timestamp, with id as the primary key and form and status unique together. I would like to get the set of rows (the status in particular) with the second-most-recent timestamp per form. I tried
SELECT form, status FROM (
SELECT form, status, timestamp
FROM formstatus
WHERE timestamp < max(timestamp)
GROUP BY form) as a
WHERE timestamp = max(timestamp)
GROUP BY form
but it didn’t work because apparently aggregates are not allowed in where clauses. So, is there a query that will return the information I am looking for?
Try this query –