Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the sum of sales per month?

How do I get the total amount per month of records from a field in a table.

I have 2 tables, customer and activity, within activity have cost (double) field and as foreign key customer id, want to show the customer and the sum of the cost given for a date.

I made the query to sum the cost value when the date is 2016-01, in this case January I want to show every month, January, February, March ....

select idcliente,
       nombre,
       sum(costo) as costo
from actividad
inner join cliente
    on actividad.cliente_idcliente = cliente.idcliente
where cliente_idcliente = 82
group by fecha=2016-01;

and that this consultation applies to all my clients not just one

EXAMPLE: i want show a table like this https://i.sstatic.net/c8VAX.jpg

like image 392
Jimmy Plage Avatar asked Jan 25 '26 05:01

Jimmy Plage


1 Answers

use month function

select 
        YEAR(fecha) as Anno
        MONTH(fecha) as Mes,
        min(idcliente),
        min(nombre),
        sum(costo) as costo 
from actividad 
        inner join cliente on actividad.cliente_idcliente=cliente.idcliente 
where cliente_idcliente=82 
group by year(fecha), month(fecha)
like image 92
Carlos Leyva Avatar answered Jan 26 '26 18:01

Carlos Leyva