title: Postgresql count last 12 month
body: <p>I am calculating the last 12-months count after joining multiple tables, my expected output is working is OK but it is not what I want?. I want to add another column with the name &quot;Current Month&quot;, so the basic idea is if I see a report for the month <strong>May</strong>, then it will <strong>start from Last year's May till This year's April</strong> and May as Current Month, total 13 columns counts. My intuition says window query will help me out on this, but I am now sure how I can do that.</p>
  <pre><code>select
  c.name,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'January'  THEN 1  END) as &quot;January&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'February' THEN 1  END) as &quot;February&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'March' THEN 1  END) as &quot;March&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'April' THEN 1  END) as &quot;April&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'May' THEN 1  END) as &quot;May&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'June' THEN 1  END) as &quot;June&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'July' THEN 1  END) as &quot;July&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'August' THEN 1  END) as &quot;August&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'September' THEN 1  END) as &quot;September&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'October' THEN 1  END) as &quot;October&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'November' THEN 1  END) as &quot;November&quot;,
    SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'December' THEN 1  END) as &quot;December&quot;
  from analytics_outbox mo 
  inner join analytics_outbox_recipient mor on mor.analytics_outbox_id = mo.id
  inner join customer c on c.id = mo.customer_id
  group by c.name
  </code></pre>
  <p><strong>Current Output:</strong></p>
  <pre><code>name                              |january|february|march |april  |may    |june|july|august|september|october|november|december|
  ----------------------------------+-------+--------+------+-------+-------+----+----+------+---------+-------+--------+--------+
  ABC                               |       |        |     1|      2|       |    |    |      |         |       |        |        |
  DEF                               |     11|      24|    34|     32|     19|    |    |      |         |       |        |        |
  GEH                               |      9|       3|     7|     18|     22|    |    |      |         |       |        |        |
  IJK                               |       |        |      |      1|       |    |    |      |         |       |        |        |
  </code></pre>