title: Postgres generate_series
body: <p>What I want to is to make statistics on a table and for this I'm using <code>generate_series();</code></p>
  
  <p>Here is what I'm doing: </p>
  
  <pre><code>SELECT x.month, amount
  FROM (SELECT generate_series(
                   min(date_trunc('month', date)),
                   max(date_trunc('month', date)),
                   '1 month'
        ) AS month
        FROM table
        WHERE user_id = 55 AND ...
  ) x
  LEFT JOIN (
        SELECT SUM(amount) AS amount, date_trunc('month', date) AS month
        FROM table
        WHERE user_id = 55 AND ...
        GROUP BY month
  ) q ON q.month = x.month
  ORDER BY month
  </code></pre>
  
  <p>This works well but when I want to apply filters like get the amount for specifics users I have to apply them twice. Is there a way to avoid filtering twice, or to rewrite this in a more efficient way because I'm not sure if it's the right way to do it?</p>