How to return the sum of column business_value, month by month, based on column start_date?

0

I have the following table:

IneedanSQLstatementthatreturnsthesumofthebusiness_valuecolumn,monthbymonth,basedonthestart_datecolumn.

Example:

I need the name of the month and the total to appear.

    
asked by anonymous 03.11.2017 / 13:56

4 answers

4
SELECT SUM(valor_negocio),
       MONTHNAME(STR_TO_DATE(MONTH(data_inicio), '%m'))
FROM negocio
GROUP BY MONTH(data_inicio)
    
03.11.2017 / 14:06
1

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)
    
03.11.2017 / 14:04
1

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

    
03.11.2017 / 14:08
-1

Good morning,

Try:

select sum(valor_negocio) from negocio group by data_inicio
    
03.11.2017 / 13:58