title: Efficient way to pivot table
body: <p>I have a table named monthly_agg which contains monthly aggregated data.</p>
  
  <pre><code>+------------+-----+----------+-----------+---------------+--------------+-------------+----------+---------+
  | yyyy_mm_dd | id  | app      | ex_status | active_status | active_count | active_base | ex_count | ex_base |
  +------------+-----+----------+-----------+---------------+--------------+-------------+----------+---------+
  | 2019-01-31 | 123 | content  | impl      | impl          | 390          | 321         | 344      | 340     |
  +------------+-----+----------+-----------+---------------+--------------+-------------+----------+---------+
  | 2019-01-31 | 333 | messages | impl      | impl          | 541          | 210         | 788      | 610     |
  +------------+-----+----------+-----------+---------------+--------------+-------------+----------+---------+
  | 2019-01-31 | 832 | photos   | no        | no            | null         | 430         | null     | 100     |
  +------------+-----+----------+-----------+---------------+--------------+-------------+----------+---------+
  </code></pre>
  
  <p>I want to make each app, be a column. Each app column should contain a percentage, which is calculated as follows:</p>
  
  <pre><code>SELECT 
      yyyy_mm_dd,
      id,
     App,
      SUM(CASE
          WHEN (app = ‘content’ AND ex_status = ‘impl’) THEN ex_count/ex_base
          WHEN (active_status = 'impl') THEN active_count/active_base
      END) AS percentage
  FROM 
      monthly_agg
  </code></pre>
  
  <p>I need to have each <code>app</code> value to be be a column and then the value of that column be the result of the above calculation. How could I pivot the table this way? Ideally my output would look like this:</p>
  
  <pre><code>+------------+-----+--------------------+---------------------+
  | yyyy_mm_dd | id  | content_percentage | messages_percentage |
  +------------+-----+--------------------+---------------------+
  | 2019-01-31 | 123 | 1.2                | null                |
  +------------+-----+--------------------+---------------------+
  | 2019-01-31 | 333 | null               | 2.57                |
  +------------+-----+--------------------+---------------------+
  </code></pre>
  
  <p>I have about 20 apps so being dynamic would be great.</p>