# Cumulative sum by month

**URL:** <https://www.ruby-forum.com/t/cumulative-sum-by-month/186934>\
**Category:** Ruby\
**Created:** [April 14, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/cumulative-sum-by-month/186934 "2010-04-14T15:47:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [April 14, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/cumulative-sum-by-month/186934/1 "2010-04-14T15:47:30Z")

</div>

Hey all,

While it is possible to doc cumulative sum by month in mysql using MONTH  
and COUNT statements, I’m trying to figure out if the same kind of  
statement is available in ruby, something like a substitute for the  
month value I put below:

def cumulative\_sum  
values = [:passed\_on, :failed\_on, :incompleted\_on]  
sum = 0  
case values  
when :passed\_on  
month.each do |state|  
while state \> ‘2010-03-00’  
@passed\_on = state.map{ |x| sum += x}  
end  
end

```
 when :failed_on
    month.each do |state|
    while state > '2010-03-00'
      @failed_on = state.map{ |x| sum += x}
    end
    end

  when :incompleted_on
     month.each do |state|
     while state > '2010-03-00'
       @incompleted_on = state.map{ |x| sum += x}
     end
     end
     end

```

end

Note that the passed\_on, failed\_on and incompleted\_on fields of students  
table are data typed as date.  
Thanks for any response.
