title: Create array in SELECT
body: <p>I'm using PostgreSQL 9.1 and I have this data structure:</p>
  
  <pre><code>A     B
  -------
  1     a
  1     a
  1     b
  1     c
  1     c
  1     c
  1     d
  2     e
  2     e
  </code></pre>
  
  <p>I need a query that produces this result:</p>
  
  <pre><code>1    4     {{c,3},{a,2},{b,1},{d,1}}
  2    1     {{e,2}}
  </code></pre>
  
  <p>A=1, 4 rows total with A=1, the partial counts (3 rows with c value, 2 rows with a value, .....)</p>
  
  <ul>
  <li>The distinct values of column "A"</li>
  <li>The count of all rows related to the "A" value</li>
  <li>An array contains all the elements related to the "A" value and the relative count of itself</li>
  </ul>
  
  <p>The sort needed for the array is based of the count of each group (like the example 3,2,1,1).</p>