title: Limit results to x groups
body: <p>I'm developing a system using Trac, and I want to limit the number of "changelog" entries returned. The issue is that Trac collates these entries from multiple tables using a <code>union</code>, and then later combines them into single 'changesets' based on their timestamp. I wish to limit the results to the latest e.g. 3 changesets, but this requires retrieving as many rows as necessary until I've got 3 unique timestamps. Solution needs to work for SQLite/Postgres.</p>
  
  <p><a href="http://trac.edgewall.org/demo-1.1/browser/branches/1.0-stable/trac/ticket/model.py#L383" rel="nofollow">Trac's current SQL</a></p>
  
  <p><strong>Current SQL Result</strong>  </p>
  
  <pre><code>Time                User  Field         oldvalue   newvalue   permanent
  =======================================================================
  1371806593507544    a     owner         b         c           1
  1371806593507544    a     comment       2         lipsum      1
  1371806593507544    a     description   foo       bar         1
  1371806593324529    b     comment       hello     world       1
  1371806593125677    c     priority      minor     major       1
  1371806592492812    d     comment       x         y           1
  </code></pre>
  
  <p><strong>Intended SQL Result (Limited to 1 timestamp e.g.)</strong></p>
  
  <pre><code>Time                User  Field         oldvalue   newvalue   permanent
  =======================================================================
  1371806593507544    a     owner         b         c           1
  1371806593507544    a     comment       2         lipsum      1
  1371806593507544    a     description   foo       bar         1
  </code></pre>