title: Group by with order by with limit per group
body: <p>I'm developing a little rss feed emailer. </p>
  
  <p>The four tables i have are Users, Feeds, Subscriptions, and Distribution. Feeds contain the actual feed details, Subscriptions connect the Users with the Feeds, and Distribution records the emails transactions of the rss feed posts. </p>
  
  <p>I'm trying to query the Distribution table to get the latest record for each user/feed subscription. I have the following query but it obviously only returns one row. I need to use a subquery but I can't wrap my head around how. </p>
  
  <pre><code>SELECT d.id, d.user_id, d.feed_id, d.created
  FROM Distribution AS d
  INNER JOIN Feeds AS f ON f.id = d.feed_id
  INNER JOIN Subscriptions AS s ON s.feed_id = d.feed_id
  GROUP BY d.id, d.user_id, d.feed_id
  ORDER BY d.created DESC 
  LIMIT 1
  </code></pre>
  
  <p>Distribution table data
  id, created, feed_id, post_id, user_id, success</p>
  
  <pre><code>(0, '2012-08-31 09:37:49', 20, 3, 2, 1)
  (1, '2012-08-25 09:36:21', 20, 1, 1, 1)
  (2, '2012-08-25 09:37:49', 21, 1, 2, 1)
  (4, '2012-08-25 09:39:06', 21, 4, 1, 1)
  (5, '2012-08-25 10:12:29', 20, 7, 2, 0)
  (6, '2011-05-24 10:34:30', 20, 112, 1, 0)
  </code></pre>
  
  <p>the following query produces the following results</p>
  
  <pre><code>SELECT Distribution.*
  FROM   Distribution NATURAL JOIN (
    SELECT   user_id, feed_id, MAX(created) AS created
    FROM     Distribution
    GROUP BY user_id, feed_id
  ) t
  
  (0, '2012-08-31 09:37:49', 20, 3, 2, 1)
  (1, '2012-08-25 09:36:21', 20, 1, 1, 1)
  (2, '2012-08-25 09:37:49', 21, 1, 2, 1)
  (4, '2012-08-25 09:39:06', 21, 4, 1, 1)
  </code></pre>