title: SUM a column when another column has equal values
body: <p>Let's suppose I have this MySQL table:</p>
  
  <pre><code>id place_id area_mq n
  -- -------- ------- --
  1  1             50 1
  
  2  2             90 1
  3  2             20 1
  
  4  3             10 1
  5  3             10 2
  6  3             10 3
  
  7  4             20 1
  8  4             20 2
  
  9  5             15 1
  10 5             25 1
  </code></pre>
  
  <p><code>id</code> is the primary key, <code>place_id</code> stands for an area id, <code>area_mq</code> is the surface area of the place in mq.</p>
  
  <p>What I have to do is find a compact query to calculate the sum of <code>area_mq</code> with these rules:</p>
  
  <ul>
  <li>if <code>n</code> is <strong>equal</strong> for the same <code>place_id</code>, then count every <code>area_mq</code> (f.ex for <code>place_id</code>=2, I count 90 <em>and</em> 20 in the sum)</li>
  <li>if <code>n</code> is <strong>different</strong> for the same <code>place_id</code>, then count <code>area_mq</code> only <em>once</em> (possible to do because for these kind of "places" the value will be the same; f.ex. <code>place_id</code>=4, there are two 20's, i sum only 20 and not 40).</li>
  </ul>
  
  <p>In this example, my correct total would be: 50 + (90 + 20) + 10 + 20 + (15 + 25).</p>
  
  <p>Can I accomplish this with a query (no code or procedures)? If requirements for n were reversed, it would be simple with a GROUP BY and a subquery... but with these conditions, I'm stuck.</p>