title: sql get most commonly used records with
body: <p>In my application each user can have his own wishlist of products.
  I'd like to get 5 most commonly used records but...
  Wishlist table gets columns:</p>
  
  <ol>
  <li>id</li>
  <li>user_id</li>
  <li>product_id</li>
  <li>position !!</li>
  </ol>
  
  <p>Position is order in which products are displayed in wishlist.
  Each position gets points eg. product in first position get 9 point, second 8 points (...) tenth position, end every next, get 0 points.</p>
  
  <p>So in other words I'd like to get 5 records with the highest value of sum (count() * position_point) gruped by product_id </p>
  
  <p>Is this possible only in sql? Or should I just use php to get correct records?</p>
  
  <p>Something like:</p>
  
  <pre><code>    select product_id, CASE position WHEN 1 THEN 9 * (select count(id) as countie from product_wishlist b where position &lt;10 and a.product_id = b.product_id and  a.position = b.position  group by product_id) end as summary
  from product_wishlist a
  </code></pre>
  
  <p>Would work correctly and is quite optimize? Just adding a rest of case condition?</p>
  
  <p>Edit: and distinct on product_id</p>