Sum Total of 2 columns MYSQL

时间:2018-12-23 15:30:25

标签: mysql

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

This query is an example of what I need, the result RecargaronRetiraron is the sum of (Recargaron + Retiraron)

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;

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"

enter image description here

这是我现在正在使用的查询,它可以正常工作,但是我觉得它必须再次创建一个COUNT表,而它只需要SUM(Recargaron + Retiraron)

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;

这只是查询的一部分,而我现在有很多列及其总计,我认为如果有这种方法可以更快地工作。

可能的解决方案:

我为我的问题找到了可能的解决方案。通过使用嵌套选择,我可以删除上一个查询中的第三个COUNT

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');

时间似乎有所改善,但并非我期望的那样。

1 个答案:

答案 0 :(得分:1)

您可以结合使用SUM(DISTINCT)和条件聚合:

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(DISTINCT (CASE WHEN Tipo IN(1,2) AND 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;

旁注:您也可以将条件Status=1直接移到WHERE句中:

SELECT DATE_FORMAT(Fecha_Procesa, '%d-%m-%y') AS Fecha,
  COUNT(DISTINCT(CASE WHEN Tipo=1 THEN Usuario END)) AS Recargaron,
  COUNT(DISTINCT(CASE WHEN Tipo=2 THEN Usuario END)) AS Retiraron,
  SUM(DISTINCT (CASE WHEN Tipo IN(1,2) THEN Usuario END)) AS RecagaronRetiraron
FROM transaccionesrr 
WHERE Fecha_Procesa BETWEEN '2018-12-01' AND '2018-12-22' 
  AND Status = 1
GROUP BY DATE_FORMAT(Fecha_Procesa, '%d-%m-%y')  
ORDER BY Fecha_Procesa;