This question has already been answered:

There is a table date | sum The date field contains the date of the DATE format (2018-01-01) The sum field contains a number (for example, 5000)

The task: to make a request for data sampling in such a way that at the output to get an array with the sum of all the sum values ​​for one month.

Example:

  • 2018-01-01 | 5000
  • 2018-01-02 | 3000
  • 2018-02-01 | 1000

The output should be:

  • January - 8000
  • February - 1000

I read a bunch of manas, forums, articles, but did not find a solution. Tell me how to do better?

Reported as a duplicate by members of Mike , Jean-Claude , mymedia , AK , Viktor Tomilov Jan 28 '18 at 13:07 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    3 answers 3

    select extract(year_month from `date`) month, sum(`sum`) from T group by extract(year_month from `date`) 

      I decided this way, it seems to work:

       SELECT SUM(sum_bill), date_format(date_bill, '%d.%m.%Y') as date_bill FROM `bill` GROUP BY MONTH(`date_bill`) 

        SELECT monthname (fullDate) AS month, SUM (amount) AS totalSales FROM your_table GROUP BY monthname (fullDate) ORDER BY month (fullDate) ASC