title: PostgreSQL - aggregate counts
body: <p>I have the following PostgreSQL table for which I am trying to get some aggregate totals for.   </p>
  
  <pre><code>number | firstused  | monthtotal 
  --------+------------+------------
  264 | 2017-11-02 |          1
  269 | 2017-11-02 |          1
  262 | 2017-01-02 |          3
  270 | 2017-11-02 |          2
  268 | 2017-10-02 |          1
  265 | 2017-04-02 |          1
  267 | 2017-11-02 |          1
  263 | 2017-01-02 |          3
  266 | 2017-04-02 |          3
  </code></pre>
  
  <p>I would like help with a query that could return a result set that looks like the following. Each row has the total of the how many monthtotals had an equal value for a year/ month. The last two rows of the example show that for 2017-11 it had 3 counts of monthtotal 1 and 1 count of monthtotal 2</p>
  
  <pre><code>firstused | monthtotal | total
  ----------+------------+------
  2017-01-02| 3          | 2
  2017-04-02| 1          | 1
  2017-04-02| 3          | 1
  2017-10-02| 1          | 1
  2017-11-02| 1          | 3
  2017-11-02| 2          | 1
  </code></pre>
  
  <p>Could a query that generates the above be also be expressed as a Hibernate criteria.</p>