# How do you total up the number of Posts per Month?

**URL:** <https://www.ruby-forum.com/t/how-do-you-total-up-the-number-of-posts-per-month/113716>\
**Category:** Rails\
**Created:** [September 4, 2007, 10:23am UTC](https://www.ruby-forum.com/t/how-do-you-total-up-the-number-of-posts-per-month/113716 "2007-09-04T10:23:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 4, 2007, 10:23am UTC](https://www.ruby-forum.com/t/how-do-you-total-up-the-number-of-posts-per-month/113716/1 "2007-09-04T10:23:42Z")

</div>

Let’s say I want to count the total number of posts per month using the  
created\_at column. For instance, I want to display a list-like format  
like so:

August (20 posts)  
July (12 posts)  
June (16 posts)

[In my controller, I have @posts = Post.find(:all)]

How would you be able to total up the # of posts per month?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 4, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/how-do-you-total-up-the-number-of-posts-per-month/113716/2 "2007-09-04T16:51:23Z")

</div>

Use find\_by\_sql, having sql query:

select created\_at, count(created\_at) as num\_of\_posts from posts  
group by created\_at  
order by created\_at desc

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 4, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/how-do-you-total-up-the-number-of-posts-per-month/113716/3 "2007-09-04T17:31:05Z")

</div>

Ivan Trajkovic wrote:

> Use find\_by\_sql, having sql query:
> 
> select created\_at, count(created\_at) as num\_of\_posts from posts  
> group by created\_at  
> order by created\_at desc

I think instead you want;

SELECT  
MONTH(created\_at) AS month,  
YEAR(created\_at) AS year,  
COUNT(\*) AS number\_of\_posts  
FROM posts  
GROUP BY year, month

you could also do this inside a normal fine if you wanted

find(:all,  
:select =\> “MONTH(created\_at) AS month, YEAR(created\_at) AS year,  
COUNT(\*) AS number of posts”,  
:group =\> “year, month”  
)
