title: Sum columns depending on another column value
body: <p>I'm having trouble with <strong>summing</strong> the fields values <strong>based on another</strong> fields value.</p>
  
  <p>I need to <code>SUM(activities.points)</code> based on <code>activities.activity_type</code> if it's <code>used_points</code> or <code>added_points</code> and put it in <code>AS used_points/added_points</code>.</p>
  
  <p>Table <em>activities</em>:</p>
  
  <pre><code>id | subscription_id  | activity_type | points
  --------------------------------------------------
  1  |         1        |  used_points  |   10
  2  |         1        |  used_points  |   50 
  3  |         1        | added_points  |   20 
  4  |         1        | added_points  |   30
  5  |         2        |  used_points  |   20
  6  |         2        |  used_points  |   45
  7  |         2        | added_points  |   45
  8  |         2        | added_points  |   45
  </code></pre>
  
  <p>Table <em>subscriptions</em>:</p>
  
  <pre><code>id |     name    |  current_points
  -------------------------------------
   1 |    card_1   |        700
   2 |    card_2   |        900
  </code></pre>
  
  <p>What I need:</p>
  
  <pre><code>  name   |  current_points  | used_points | added_points
  -----------------------------------------------------------
   card_1  |         700      |     60      |     50
   card_2  |         900      |     65      |     90
  </code></pre>
  
  <p>What I tried :</p>
  
  <pre><code>SELECT
      subscriptions.name,
      subscriptions.current_points,
      IF(activities.activity_type="used_points", SUM(activities.points), null)
          AS used_points,
      IF(activities.activity_type="added_points", SUM(activities.points), null)
          AS added_points 
  FROM activities
  JOIN subscriptions
      ON activities.subscription.id = subscription.id
  GROUP BY subscriptions.name
  </code></pre>
  
  <p>Which is wrong.</p>
  
  <p>Thanks</p>