title: Join on generate_series and count
body: <p>I'm trying to find the # users who did action A or action B on a monthly basis.</p>
  
  <p>Table: User
  - id
  - "creationDate"</p>
  
  <p>Table: action_A
  - user_id (= user.id)
  - "creationDate"</p>
  
  <p>Table: action_B
  - user_id (= user.id)
  - "creationDate"</p>
  
  <p>The general idea of what I was trying to do was that I'd find the list of users who did action A in Month X and the list of users who did action B in Month X, then count how many ids are there for every month based on a generate_series of monthly dates.</p>
  
  <p>I tried the following, however, the query times out when running and I'm not sure if there's any way to optimize it (or if it is even correct). </p>
  
  <pre><code>SELECT monthseries."Month", count(*)
  FROM
    (SELECT to_char(DAY::date, 'YYYY-MM') AS "Month"
     FROM generate_series('2014-01-01'::date, CURRENT_DATE, '1 month') DAY) monthseries
  LEFT JOIN
    (SELECT to_char("creationDate", 'YYYY-MM') AS "Month",
            id
     FROM action_A) did_action_A ON monthseries."Month" = did_action_A."Month"
  LEFT JOIN
    (SELECT to_char("creationDate", 'YYYY-MM') AS "Month",
            id
     FROM action_B) did_action_B ON monthseries."Month" = did_action_B."Month"
  GROUP BY monthseries."Month"
  </code></pre>
  
  <p>Any comments/ help would be immensely helpful! </p>