Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Group and sum while adding last values (Fibonacci)

I'm trying to group quotes sum per month, while adding last sums.

For example:

Jan: 300€
Fev: 200€
Mars: 100€

What the group should return is:

Jan:300€
Fev: 500€ (200 + Jan's 300)
Mars: 600€ (100 + Fev's 500)

Current SQL:

current_user.quotes.group_by_month(:created_at, last: 12).sum(:price)

I'm using the groupdate gem.

Thanks.

like image 635
Yassine Avatar asked Jan 29 '26 00:01

Yassine


2 Answers

If used postgresql you can use windows function

  1. UNBOUNDED PRECEDING first row,CURRENT ROW current rows
  2. use sum function to Accumulate

like this.

select name,sum(price) 
         over(order by (select 1) ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)  "price"
from T

sqlfidde:http://sqlfiddle.com/#!17/763e3/7

Widows function

like image 103
D-Shih Avatar answered Jan 30 '26 12:01

D-Shih


Solution in Ruby:

s = 0
current_user.quotes.group_by_month(:created_at, last: 12).
  sum(:price).
  transform_values.map { |v| s += v }

as there is only a hash of 12 elements, i suggest the performance penalty of using ruby instead of sql are marginal. Using SQL would be much harder and not possible only with simple ActiveRecord Arel methods or probably database specific, see e.g. Calculating Cumulative Sum in PostgreSQL

like image 33
stwienert Avatar answered Jan 30 '26 14:01

stwienert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!