title: How to groupby in subquery SQL
body: <p>I have the following query:</p>
  <pre><code>SELECT &quot;IMPORTACIONCOLUMN3&quot;, &quot;role&quot;, COUNT(&quot;IMPORTACIONCOLUMN1&quot;)
  FROM MYTABLE
  WHERE ID = 9
  GROUP BY &quot;IMPORTACIONCOLUMN3&quot;, &quot;role&quot;
  </code></pre>
  <p>This gives me the following result:</p>
  <p><a href="https://i.stack.imgur.com/iQWvO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iQWvO.png" alt="enter image description here" /></a></p>
  <p>I would like to achieve the following:</p>
  <ol>
  <li>The &quot;unique&quot; values are grouped together (i.e. Instead of having 4 values of &quot;Robot 1&quot; these are grouped togehter in just 1 cell summing the count values.</li>
  <li>The second group by or subquery has to be the same count, but with role instead of &quot;IMPORTACIONCOLUM3&quot;</li>
  <li>Is it possible (for the second picture) to &quot;link&quot; the values either by index or adding an extra column to reference them (i.e. There's two &quot;Solicitante&quot; with a count value of &quot;52&quot; but it refers to &quot;Robot 1&quot; and other to &quot;Solicitante&quot; with count value of &quot;58&quot; links to &quot;Robot 2&quot;</li>
  </ol>
  <p>The second image represent visually what I'm trying to explain.</p>
  <p><a href="https://i.stack.imgur.com/DA2W2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DA2W2.png" alt="enter image description here" /></a></p>
  <p>I have been trying on my own but only have reached the following:</p>
  <pre><code>select &quot;IMPORTACIONCOLUMN3&quot;, count(&quot;IMPORTACIONCOLUMN1&quot;)
  from
  (
     select &quot;IMPORTACIONCOLUMN1&quot;, count(&quot;role&quot;), &quot;IMPORTACIONCOLUMN3&quot;
     from MYTABLE 
     WHERE ID = 9
     group by &quot;IMPORTACIONCOLUMN1&quot;, &quot;IMPORTACIONCOLUMN3&quot;
  ) as tmp
  group by &quot;IMPORTACIONCOLUMN3&quot;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/xF2W3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xF2W3.png" alt="enter image description here" /></a></p>
  <p>But it is not yet the result I am looking for.</p>
  <p>Thanks in advance for your help and tips!</p>
  <p>EDIT:</p>
  <p>Explaining my desired output in detail
  Each one of &quot;Robot 1, 2, 3&quot; have roles such as &quot;Solicitante&quot;, &quot;Gerente&quot;, etc. with different values.</p>
  <p>i.e. The first row &quot;Humano&quot; value &quot;243&quot; is the sum of &quot;Agente de Compras - 95&quot;, &quot;Gerente Financiero - 37&quot;, &quot;Gerente Solicitante - 45&quot;, &quot;Proovedor - 31&quot;, &quot;Solicitante - 60&quot;.
  I am linking these by the column &quot;GRAFICOCOLUMNARECURSIVOID&quot; with contains the index of whatever &quot;Robot&quot; these &quot;roles&quot; are from.</p>
  <p>I would like to achieve a query containing subquerys that allows me to have this output.</p>
  <p><a href="https://i.stack.imgur.com/VJByH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VJByH.png" alt="enter image description here" /></a></p>