title: Get rows with second-most-recent timestamp by category?
body: <p>I have a MySQL table (called <code>formstatus</code>) with 4 columns: <code>id</code>, <code>form</code>, <code>status</code>, and <code>timestamp</code>, with <code>id</code> as the primary key and <code>form</code> and <code>status</code> unique together. I would like to get the set of rows (the <code>status</code> in particular) with the second-most-recent timestamp per <code>form</code>. I tried</p>
  
  <pre><code>SELECT form, status FROM (
    SELECT form, status, timestamp
    FROM formstatus
    WHERE timestamp &lt; max(timestamp)
    GROUP BY form) as a
  WHERE timestamp = max(timestamp)
  GROUP BY form
  </code></pre>
  
  <p>but it didn't work because apparently aggregates are not allowed in where clauses. So, is there a query that <em>will</em> return the information I am looking for?</p>