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.
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);
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With