title: Select top N rows out of M groups
body: <p>I have this table:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `catalog_sites` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `cat_id` int(10) unsigned NOT NULL,
    `date` datetime NOT NULL,
    `url` varchar(255) NOT NULL,
    `title` varchar(255) NOT NULL,
    `description` varchar(255) NOT NULL,
    `keywords` varchar(255) NOT NULL,
    `visited` int(10) unsigned NOT NULL,
    `shown` int(10) unsigned NOT NULL,
    `meta_try` int(1) NOT NULL,
    PRIMARY KEY  (`id`),
    UNIQUE KEY `url` (`url`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  </code></pre>
  
  <p>I think my problem is simple, but cant seem to find an appropriate solution..</p>
  
  <p>So, this is a table with web-sites, I would like to get 6 sites in 6 different categories (cat_id, total: 36 rows) with the highest rating for each category. The rating is calculated as <code>visited</code> / <code>shown</code>.</p>
  
  <p>I should get 36 rows containing 6 top categories (we can find them by sorting with <code>AVG(visited / shown)</code> ), and 6 top sites in each of these 6 categories.</p>
  
  <p>If you have any ideas how this might happen differently, please tell me.</p>