Add total column and group by month in mysql

3

How do I add the total column and group by the column mes , but do not add me when the id_orden is repeated (218), and try to group them by month, and it looks like this:
The sum of month 5 is 10 and the sum of month 1 gives 240, and I want the sum of month 1 to be 190 since the order_id 218 is repeated.

    
asked by Juan David Delgado J 17.05.2018 в 05:24
source

3 answers

3

I propose this answer

select id_orden, mes, producto, sum(distinct total) as total
from ventas where id_orden not in(218) group by mes;

And this is the result

+----------+-----+----------+-------+
| id_orden | mes | producto | total |
+----------+-----+----------+-------+
|      214 |   1 | reloj    |   140 |
|      210 |   5 | monitor  |    10 |
+----------+-----+----------+-------+
    
answered by 17.05.2018 в 05:40
0

Maybe it works for you:

SELECT id_orden, mes, producto, sum(total) as total
       FROM ventas 
       WHERE id_order in(SELECT DISTINCT id_order FROM ventas)
       GROUP BY mes;

Greetings.

    
answered by 17.05.2018 в 08:43
0

This was my solution:

SELECT id_orden ,
       fecha_transaccion, 
       month(fecha_transaccion) AS MES ,
       producto,sum(total) 
FROM (
      SELECT id_orden ,
             fecha_transaccion, 
             month(fecha_transaccion) AS MES ,
             producto,total 
      FROM 'ingresos_ecommerce'  
      WHERE email_destino = "[email protected]" AND 
            ING.tipo = "virtual" 
      GROUP BY id_orden, month(fecha_transaccion)
     ) 
AS TAB GROUP BY month(fecha_transaccion) 
    
answered by 17.05.2018 в 14:52