title: Sum Total of 2 columns MYSQL
body: <p>I'm trying to improve my queries by making them work with the result they already have to make a total SUM of two columns</p>
  
  <p>This query is an example of what I need, the result <code>RecargaronRetiraron</code> is the sum of (<code>Recargaron + Retiraron</code>)</p>
  
  <pre><code>SELECT 
      DATE_FORMAT(Fecha_Procesa, '%d-%m-%y') AS Fecha,
      COUNT(DISTINCT(CASE WHEN Tipo=1 AND Status =1 THEN Usuario END )) AS Recargaron,
      COUNT(DISTINCT(CASE WHEN Tipo=2 AND Status =1 THEN Usuario END )) AS Retiraron,
      SUM(Recargaron + Recargaron) AS RecargaronRetiraron
  FROM 
      transaccionesrr 
  WHERE 
      Fecha_Procesa BETWEEN '2018-12-01' AND '2018-12-22' 
  GROUP BY 
      DATE_FORMAT(Fecha_Procesa, '%d-%m-%y')  
  ORDER BY 
      Fecha_Procesa;
  </code></pre>
  
  <p>This is the result that i get, it have a GROUP BY each day, i want the SUM of this two columns in the column "Total"</p>
  
  <p><a href="https://i.stack.imgur.com/SS17E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SS17E.png" alt="enter image description here"></a></p>
  
  <p>This is the query I'm using right now which work but I feel that it have to make a <code>COUNT</code> again the table and it just need to <code>SUM(Recargaron + Retiraron)</code></p>
  
  <pre><code>SELECT 
      DATE_FORMAT(Fecha_Procesa, '%d-%m-%y') AS Fecha,
      COUNT(DISTINCT(CASE WHEN Tipo=1 AND Status =1 THEN Usuario END )) AS Recargaron,
      COUNT(DISTINCT(CASE WHEN Tipo=2 AND Status =1 THEN Usuario END )) AS Retiraron,
      COUNT(DISTINCT(CASE WHEN Status =1 THEN Usuario END )) AS RecargaronRetiraron,
  FROM 
      transaccionesrr 
  WHERE 
      Fecha_Procesa BETWEEN '2018-12-01' AND '2018-12-22' 
  GROUP BY 
      DATE_FORMAT(Fecha_Procesa, '%d-%m-%y')  
  ORDER BY 
      Fecha_Procesa;
  </code></pre>
  
  <p>This is just a part of the query the one that i have right now have a lot of columns with their totals and i think it should work faster if there is a way to do it like this.</p>
  
  <p><strong>POSSIBLE SOLUTION:</strong></p>
  
  <p>I found possible solution for my question. By using nested select I was able to remove the third COUNT that i had in my previous query</p>
  
  <pre><code>SELECT 
      res.fecha, 
      res.Recargaron, 
      res.Retiraron,
      SUM(res.Recargaron + res.Retiraron) as RecargaronRetiraron
  FROM
      (SELECT 
           DATE_FORMAT(Fecha_Procesa, '%d-%m-%y') AS Fecha,
           COUNT(DISTINCT(CASE WHEN Tipo=1 AND Status =1 THEN Usuario END )) AS Recargaron,
           COUNT(DISTINCT(CASE WHEN Tipo=2 AND Status =1 THEN Usuario END )) AS Retiraron
       FROM 
           transaccionesrr 
       WHERE 
           Fecha_Procesa BETWEEN '2018-12-01' AND '2018-12-22' 
       GROUP BY 
           DATE_FORMAT(Fecha_Procesa, '%d-%m-%y') 
       ORDER BY 
           Fecha_Procesa) AS res
  GROUP BY 
      DATE_FORMAT(res.fecha, '%d-%m-%y');
  </code></pre>
  
  <p>There seems to be an improvement in time but is not what I expected it to be.</p>