title: SQL logic: sort result by product of a column and number of colums of a specific type
body: <p>A catalog I have users can sort through by review score. Here's the SQL for grabbing the catalog by review score:</p>
  
  <pre><code>   SELECT *, null AS score, '0' AS SortOrder
          FROM products
          WHERE datelive &lt; 0
     UNION
     SELECT p.*, ROUND(SUM(r.score)/COUNT(*)) AS score, '1' AS SortOrder
          FROM products p
          LEFT JOIN reviews r
              ON r.id = p.id
              AND p.datelive &gt; 0
          GROUP BY p.id
          HAVING COUNT(*) &gt;= 5
      UNION
      SELECT p.*, null AS score, '2' AS SortOrder
          FROM products p
          LEFT JOIN reviews r
              ON r.id = p.id
              WHERE p.datelive &gt; 0
          GROUP BY p.id
          HAVING COUNT(*) &lt; 5
      ORDER BY SortOrder ASC, score DESC
  </code></pre>
  
  <p>What it does is grab the following in order:</p>
  
  <ul>
  <li>products of type datelive &lt; 0</li>
  <li>products of type datelive > 0 and >= 5 reviews</li>
  <li>products of type datelive > 0 and &lt; 5 reviews</li>
  </ul>
  
  <p>The entire thing is also sorted by review score. I want to sort the middle bullet point by the score as it is, but multiplied by the percentage of reviews with score >= 3.53.</p>
  
  <p>Say a product has a raw review score of 4.53, but two out of twenty reviews are below 3.53, that's 90% of reviews which are >= 3.53 scored. I want to sort it by <code>(score*mult)</code> where <code>mult</code> is the number of reviews >= 3.53 divided by total number of reviews. In this case, the score I'm sorting by is <code>4.53 * 0.9 = 4.08</code>.</p>