# Cumulative sum using mysql/rails

**URL:** <https://www.ruby-forum.com/t/cumulative-sum-using-mysql-rails/186474>\
**Category:** Rails\
**Created:** [April 7, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/cumulative-sum-using-mysql-rails/186474 "2010-04-07T16:48:36Z")\
**Posts on this page:** 4\
**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 7, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/cumulative-sum-using-mysql-rails/186474/1 "2010-04-07T16:48:36Z")

</div>

I’m having an issue generating a cumulative sum by month in rails. This  
is what I have:

@results = Student.connection.select\_all(“SELECT  
x1.MonthNo  
, x1.MonthName  
, x1.Added  
, SUM(x2.Added) AS RunningTotal  
FROM  
(  
SELECT  
MONTH(passed\_on) AS MonthNo  
, MONTHNAME(passed\_on) AS MonthName  
, COUNT(_) AS Added  
FROM students  
WHERE passed\_on \>= ‘2009-09-23’  
GROUP BY MONTH(passed\_on)  
) AS x1  
INNER JOIN (  
SELECT  
MONTH(passed\_on) AS MonthNo  
, MONTHNAME(passed\_on) AS MonthName  
, COUNT(_) AS Added  
FROM students  
WHERE passed\_on \>= ‘2009-09-23’  
GROUP BY MONTH(passed\_on)  
) AS x2  
ON x1.MonthNo \>= x2.MonthNo  
GROUP BY x1.MonthNo;”)

@result = @results.map{ |result| result[:RunningTotal] }

@result returns this:

[nil, nil]

even though @results returns this:

[{“RunningTotal”=\>“1”, “MonthName”=\>“February”, “Added”=\>“1”,  
“MonthNo”=\>“2”}, {“RunningTotal”=\>“11”, “MonthName”=\>“March”,  
“Added”=\>“10”, “MonthNo”=\>“3”}]

I would like @result to return this:  
[1,11]

Any suggestions thanks?

---

<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 7, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/cumulative-sum-using-mysql-rails/186474/2 "2010-04-07T17:03:18Z")

</div>

On Apr 7, 3:48 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would like @result to return this:  
> [1,11]

> Any suggestions thanks?

Symbols and strings are not the same thing

Fred

–

---

<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 7, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/cumulative-sum-using-mysql-rails/186474/3 "2010-04-07T17:14:07Z")

</div>

Frederick C. wrote:

> On Apr 7, 3:48ï¿½pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I would like @result to return this:  
> > [1,11]
> 
> > Any suggestions thanks?
> 
> Symbols and strings are not the same thing
> 
> Fred
> 
> –

But even this:

@result = @results.map{ |result| result[‘RunningTotal’] }

returns this:

[“1”, “11”]

Which is not helping me either, because I believe I need something like  
this:  
[1,11]  
for the chart I’m using.

---

<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 7, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/cumulative-sum-using-mysql-rails/186474/4 "2010-04-07T17:19:31Z")

</div>

Nevermind I figured it out. I just need to\_i
