Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql select sum group by date

People also ask

How do I SUM after GROUP BY in SQL?

SUM is used with a GROUP BY clause. The aggregate functions summarize the table data. Once the rows are divided into groups, the aggregate functions are applied in order to return just one value per group. It is better to identify each summary row by including the GROUP BY clause in the query resulst.

How do I find the date in a GROUP BY?

Cast the datetime to a date, then GROUP BY using this syntax: SELECT SUM(foo), DATE(mydate) FROM a_table GROUP BY DATE(a_table. mydate);

What is rollup in MySQL?

The GROUP BY clause permits a WITH ROLLUP modifier that causes summary output to include extra rows that represent higher-level (that is, super-aggregate) summary operations. ROLLUP thus enables you to answer questions at multiple levels of analysis with a single query.


This solution will give you the month name as a column of your resultset, followed by the total as required.

SELECT MONTHNAME(o_date), SUM(total) 
FROM theTable
GROUP BY YEAR(o_date), MONTH(o_date)

select year(o_date), month(o_date), sum(total)
from table
group by year(o_date), month(o_date);

Get Month And Year wise data from MySQL database:

SELECT MONTHNAME(o_date), YEAR(o_date), SUM(total) 
FROM the_table 
GROUP BY YEAR(date), MONTH(date)

SELECT SUM(total)
FROM table
GROUP BY MONTH(o_date)