# How to get missing months

**URL:** <https://www.ruby-forum.com/t/how-to-get-missing-months/145145>\
**Category:** Rails\
**Created:** [August 6, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/how-to-get-missing-months/145145 "2008-08-06T13:18:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [August 6, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/how-to-get-missing-months/145145/1 "2008-08-06T13:18:33Z")

</div>

Hi,  
i am using a Rails count method like this :  
start\_date = Date.civil(y=2008,m=1)  
@users\_count = User.count(:all, :conditions =\> [“last\_updated BETWEEN ?  
and ?”, start\_date.\>\>(Date.today.month - 8), Date.today], :group =\>  
“DATE\_FORMAT(last\_updated, ‘%b %Y’)”, :order =\> :last\_updated)

getting output like this

[[“Jan 2008”, 30], [“Feb 2008”, 60], [“Mar 2008”, 70], [“May 2008”, 46],  
[“Jun 2008”, 33], [“Aug 2008”, 10]]

but i was missing the months Apr,Jul when there were no records in that  
months  
Can we get the 2 months when there was no data in that months as [“Apr  
2008”, nil], [“Jul 2008”, nil] or in any way ?

any help ??

thanks

---

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [August 6, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/how-to-get-missing-months/145145/2 "2008-08-06T19:26:14Z")

</div>

On Wed, Aug 06, 2008 at 01:18:33PM +0200, Srinath A. wrote:

> but i was missing the months Apr,Jul when there were no records in that  
> months  
> Can we get the 2 months when there was no data in that months as [“Apr  
> 2008”, nil], [“Jul 2008”, nil] or in any way ?
> 
> any help ??

Don’t try to do it in the query itself, just post-process in Ruby:

module DateStepMonth  
def succ  
(Date.civil(year, month, -1) + 1).extend(DateStepMonth)  
end  
end

first\_month = Date.strptime(@users\_count.first.first, “%b %Y”)  
last\_month = Date.strptime(@users\_count.last.first, “%b %Y”)  
first\_month.extend(DateStepMonth)  
last\_month.extend(DateStepMonth)  
hash\_counts = @user\_counts.inject({}) { |h,(month,count)|  
h[Date.strptime(month, “%b %Y”)] = count; h  
}  
@user\_counts = (first\_month…last\_month).map { |month|  
[month.strftime("%b %Y"), hash\_counts(month)]  
}

> thanks  
> –Greg

---

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [August 7, 2008, 1:01pm UTC](https://www.ruby-forum.com/t/how-to-get-missing-months/145145/3 "2008-08-07T13:01:05Z")

</div>

Gregory S. wrote:

> On Wed, Aug 06, 2008 at 01:18:33PM +0200, Srinath A. wrote:
> 
> > but i was missing the months Apr,Jul when there were no records in that  
> > months  
> > Can we get the 2 months when there was no data in that months as [“Apr  
> > 2008”, nil], [“Jul 2008”, nil] or in any way ?
> > 
> > any help ??
> 
> Don’t try to do it in the query itself, just post-process in Ruby:
> 
> module DateStepMonth  
> def succ  
> (Date.civil(year, month, -1) + 1).extend(DateStepMonth)  
> end  
> end
> 
> first\_month = Date.strptime(@users\_count.first.first, “%b %Y”)  
> last\_month = Date.strptime(@users\_count.last.first, “%b %Y”)  
> first\_month.extend(DateStepMonth)  
> last\_month.extend(DateStepMonth)  
> hash\_counts = @user\_counts.inject({}) { |h,(month,count)|  
> h[Date.strptime(month, “%b %Y”)] = count; h  
> }  
> @user\_counts = (first\_month…last\_month).map { |month|  
> [month.strftime("%b %Y"), hash\_counts(month)]  
> }
> 
> > thanks  
> > –Greg

hi Greg thanks for the Reply,

but i was geting error

3 elements of civil date are necessary

RAILS\_ROOT: script/…/config/…  
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/1.8/date.rb:650:in `new_with_hash' /usr/lib/ruby/1.8/date.rb:675:in`strptime’  
app/models/report.rb:51:in `track_users_chart' app/controllers/reporting_controller.rb:29:in`track\_users’  
-e:4:in `load’  
-e:4  
any idea ??

thanks ,  
srinath

---

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [August 7, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/how-to-get-missing-months/145145/4 "2008-08-07T16:23:28Z")

</div>

> What version of Ruby are you using? I tested this in 1.8.6. Maybe  
> strptime  
> needs a day of the month as well in the version you have. Try adding "  
> 1"  
> to the end of the string being parsed and " %d" to the end of the format  
> string.
> 
> > thanks ,  
> > srinath  
> > –Greg

i am using ruby - 1.8.5 an rails - 1.2.3.

yep you are right if i add like that i was getting the output, but the  
months were returning nil values if there were no records on that day.  
as i was formating all the dates in one month to a single month name say  
“[Jan 2008, 12]” or else i will get more values for each month like  
“[Jan 12 2008, 4]”,"[Jan 20 2008, 6]","[Jan 25 2008, 2]" this doesnt  
work for me !

is there any alternative for this in ruby 1.8.5 ?

help me !  
thanks  
srinath

---

<div class="post-metadata">

**Author:** ![srinathbtech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@srinathbtech](https://www.ruby-forum.com/u/srinathbtech)\
**Post date:** [August 7, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/how-to-get-missing-months/145145/5 "2008-08-07T15:18:00Z")

</div>

On Thu, Aug 07, 2008 at 01:01:05PM +0200, Srinath A. wrote:

> > first\_month.extend(DateStepMonth)
> 
> /usr/lib/ruby/1.8/date.rb:675:in `strptime' app/models/report.rb:51:in`track\_users\_chart’  
> app/controllers/reporting\_controller.rb:29:in `track_users' -e:4:in`load’  
> -e:4  
> any idea ??

What version of Ruby are you using? I tested this in 1.8.6. Maybe  
strptime  
needs a day of the month as well in the version you have. Try adding "  
1"  
to the end of the string being parsed and " %d" to the end of the format  
string.

> thanks ,  
> srinath  
> –Greg
