title: MySql get sum of a subquery, the subquery has a union
body: <p>I'm new to mysql but not new to sql.  Below is my code and error message</p>
  
  <blockquote>
    <p>Error: 42000][1064] You have an error in your SQL syntax; check the
    manual that corresponds to your MariaDB server version for the right
    syntax to use near 'select content_type, count(*) as Red, 0 as Green
    from touchpoints ' at line 3</p>
  </blockquote>
  
  <pre><code>SELECT content_type, SUM(Red), SUM(Green)
  FROM (
           select content_type, count(*) as Red, 0 as Green
           from touchpoints
           where evaluatee_user_id = 6
             and color_code = 'R'
           group by content_type
           UNION
           select content_type, 0 as Red, count(*) as Green
           from touchpoints
           where evaluatee_user_id = 6
             and color_code = 'G'
           group by content_type
      )
  GROUP BY content_type
  </code></pre>
  
  <p>The subquery works and the output is:</p>
  
  <pre><code>M   1   0
  SCI 5   0
  SFA 3   0
  SS  1   0
  M   0   3
  </code></pre>