I have the following table:
IneedanSQLstatementthatreturnsthesumofthebusiness_valuecolumn,monthbymonth,basedonthestart_datecolumn.
Example:
I need the name of the month and the total to appear.
SELECT SUM(valor_negocio),
MONTHNAME(STR_TO_DATE(MONTH(data_inicio), '%m'))
FROM negocio
GROUP BY MONTH(data_inicio)
Complementing Henry's response:
SELECT SUM(valor_negocio) AS SOMA, MONTH(data_inicio) AS MES
FROM negocio GROUP BY MONTH(data_inicio)
In addition, if the selected data is greater than one year, you also need to group by year:
SELECT SUM(valor_negocio) AS SOMA, MONTH(data_inicio) AS MES
FROM negocio GROUP BY MONTH(data_inicio), YEAR(data_inicio)
Separating by years, but without displaying them:
Select
MONTHNAME(DATE_FORMAT(data_inicio,'%Y-%m-01')) as mes,
sum(valor_negocio)
from negocio
group by DATE_FORMAT(data_inicio,'%Y-%m-01')
order by DATE_FORMAT(data_inicio,'%Y-%m-01');
Displaying the years:
Select
Year(data_inicio) as ano,
MONTHNAME(DATE_FORMAT(data_inicio,'%Y-%m-01')) as mes,
sum(valor_negocio)
from negocio
group by ano, DATE_FORMAT(data_inicio,'%Y-%m-01')
order by DATE_FORMAT(data_inicio,'%Y-%m-01');
I put it in SQLFiddle: link
Good morning,
Try:
select sum(valor_negocio) from negocio group by data_inicio