Blankman
Blankman

Reputation: 266998

How to get a summary of Posts by month? i.e. month with count

I need to get a list of months that contain Posts in the Post table, along with the count of posts for the month.

e.g.

JAN 11
FEB 23
MAR 4

So some months won't show up if there are no posts for that month.

My post model is like:

Post (ID, TITLE, DATE_CREATED)

I don't need the actual posts returned, just the MONTH and the COUNT for the month.

Upvotes: 1

Views: 267

Answers (1)

mpapis
mpapis

Reputation: 53158

Easiest way probably (rails2 not sure about 3 but should be similar):

Post.count(:group=>'MONTH(DATE_CREATED)')

Upvotes: 3

Related Questions