title: Grouping together values in SQL
body: <p>I have a postgresql database with data having four columns. I am using sqllite3 to run these commands in python. Say the columns are named A, B, c, d. 
  Each event in the database is uniquely represented by combined values of A and B. As an example, consider the table below. </p>
  
  <pre><code>--------------------------------------------------
  |  A| B|   c|   d|
  --------------------------------------------------
  | 10| 3|  c1|  d1|
  --------------------------------------------------
  | 11| 4|  c2|  d2|
  --------------------------------------------------
  | 10| 3|  c3|  d3|
  --------------------------------------------------
  | 12| 3|  c4|  d4|
  --------------------------------------------------
  | 13| 1|  c5|  d5|
  --------------------------------------------------
  | 10| 3|  c6|  d6|
  --------------------------------------------------
  | 11| 4|  c7|  d7|
  --------------------------------------------------
  | 10| 1|  c8|  d8|
  --------------------------------------------------
  | 10| 2|  c9|  d9|
  --------------------------------------------------
  | 11| 3| c10| d10|
  --------------------------------------------------
  </code></pre>
  
  <p>As you can see, the combination 10 and 3 appears three times in the table meaning all those rows were part of the same event (where each event is uniquely represented by combined value in A and B). What I want out of this is a list of all the details:</p>
  
  <ol>
  <li>grouped according the column A</li>
  <li><em>then</em> grouped according to it's corresponding value in the column B. </li>
  </ol>
  
  <p>For example, in this case, <code>(c1, c3, c6), (d1, d2, d6), (c8), (d8), (c9), (d9)</code> - these correspond to 10 in Column A. The first two 'groups' came from 10 (from A) and the corresponding 3 (from B). The second two came from 10(A) and 1(B), and the last two came from 10(A) and 2(B). Similarly everything else also has to be grouped in this manner. </p>
  
  <p>I am a complete noob to SQL so I am really lost. After much googling I got to this part </p>
  
  <pre><code>SELECT GROUP_CONCAT(c, ',') c,\
         GROUP_CONCAT(d, ',') d,\
  FROM table WHERE GROUP BY A, B
  </code></pre>
  
  <p>This is giving me the details but it groups A and B together and so the output that I get has 10/3 and 10/2 in a different row. I want all the 10s (and all the other values in A) to be grouped together and <em>then</em> grouped by it's value in B. </p>
  
  <p>How can this be done? Thanks!</p>
  
  <p>EDIT: The required answer in tabular format- </p>
  
  <pre><code>--------------------------------------------------
  |  (c1, c3, c6), (d1, d3, d6), (c8), (d8), (c9), (d9)
  --------------------------------------------------
  | (c2, c7), (d2, d7), (c10), (d10)
  --------------------------------------------------
  | (c4), (d4)
  --------------------------------------------------
  | (c5), (d5)
  --------------------------------------------------
  </code></pre>