Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count totals by year and month

I have a table that looks like this:

id,created,action
1,'2011-01-01 04:28:21','signup'
2,'2011-01-05 04:28:21','signup'
3,'2011-02-02 04:28:21','signup'

How do I select and group these so the output is:

year,month,total
2011,1,2
2011,2,1
like image 353
Tom Avatar asked Mar 02 '11 10:03

Tom


People also ask

How to count occurrences per month Excel?

Count the number of occurrences per year/month with formulas Select a blank cell you will place the counting result at, and type the formula =SUMPRODUCT((MONTH($A$2:$A$24)=F2)*(YEAR($A$2:$A$24)=$E$2)) into it, and then drag this cell's AutoFill handle down to the range as you need.

How do I count monthly records in SQL?

Calculate Monthly Sales Report in MySQL If you only want a total count of sales every month, then you can use COUNT function instead. mysql> select year(order_date),month(order_date),sum(sale) from sales WHERE condition group by year(order_date),month(order_date) order by year(order_date),month(order_date);


1 Answers

Try this:

SELECT DATE_FORMAT(created, '%Y') as 'year',
DATE_FORMAT(created, '%m') as 'month',
COUNT(id) as 'total'
FROM table_name
GROUP BY DATE_FORMAT(created, '%Y%m')
like image 53
Adam Lukaszczyk Avatar answered Sep 22 '22 05:09

Adam Lukaszczyk