title: SQL group by values stored in arrays
body: <p>I use OrientDB.
  I have a table like this:</p>
  
  <pre><code> NAME | CATEGORIES
  -------------------
    N1  |   [A,B]
    N2  |   [C]
    N3  |   [C,A]
    N4  |   [A,B]
  </code></pre>
  
  <p>And I would like to build a query that returns a list of categories, and for each category a list of related names, like this:</p>
  
  <pre><code> CATEGORY |   NAMES
  -----------------------
      A     | [N1,N3,N4]
      B     | [N1,N4]
      C     | [N2,N3]
  </code></pre>
  
  <p>If "categories" wasn't an array, I could achieve it with:</p>
  
  <pre><code>SELECT
    Categories as Category,
    set(Name) as Names
  FROM Table
  GROUP BY Categories
  </code></pre>
  
  <p>But, being Categories arrays, this is what I get:</p>
  
  <pre><code> CATEGORY |  NAMES
  --------------------
     [A,B]  | [N1,N4]
      [C]   | [N2]
     [C,A]  | [N3]
  </code></pre>
  
  <p>What query should I rather write?</p>