Ruby on Rails Query with Sum and Group

how to group by and sum in rails 4

@costs = @trip.costs.where(user_id: current_user).group("exchange_id"))
@costs_sum = @costs.sum('the_field_you_want_to_sum_by')
=> { "exchange_id_1" => sum_1, ... }

PS: there is a typo.

select sum of grouped rows in activerecord

Single-quotes are messing this up for you, this should work

User.find(2).baskets
.joins(:line_items)
.group('baskets.id')
.order('baskets.date desc')
.select("baskets.date, sum(line_items.quantity) as quantity_sum")

You can also use pluck which will return an array of date and sum

User.find(2).baskets
.joins(:line_items)
.group('baskets.id')
.order('baskets.date desc')
.pluck("baskets.date, sum(line_items.quantity) as quantity_sum")

Rails: Sum column by group

Figured it out.

Controller

def index
@votes = Vote.all
@originalitysum = Vote.select([:widget_id, :originality]).group('widget_id').sum(:originality)
@votecount = Vote.group(:widget_id).count(:originality)
@votes = Vote.select(:widget_id,
"SUM(originality) as originality_sum",
"SUM(interest) as interest_sum",
"SUM(rating) as rating_sum").group(:widget_id).order("rating_sum DESC")
end

The view

<% @votes.group(:burger_id).each do |vote| %>
<tr>
<td><%= vote.widget.name %></td>
<td><%= vote.widget.store %></td>
<td><%= vote.originality_sum %></td>
<td><%= vote.interest_sum %></td>
<td><%= vote.rating_sum %></td>
</tr>
<% end %>

Thanks to this answer in this link, I was able to parse it together.
Group by a column and then get the sum of another column in Rails

The added bonus is that it allowed me to easily sum the other columns as well.

Get sum of a group by count

You won't be able to do this in a single SQL query unless you craft some fancy SQL yourself. If performance is not a problem, I would aggregate this on the controller:

counts = User.group(:city).count
total = counts.values.sum

Group by and Sum in Rails 3

Hey you can used having clause as

Demo.group('id').having("sum(percentage) < 100")

Rails - How to order query of sum() and group()?

You can sort outside of ActiveRecord by using sort_by of the Enumerable mixins:

Payment.joins(:project, :pr_category).select("sum(payments.amount) as Total, projects.name as Project, definitions.name as Category, payments.project_id as project_id").group('payments.project_id, payments.pr_category_id').sort_by { |p| c.project_id }

How to sum records grouped by month in Ruby on Rails

A very simple solution would be to use the gem GroupDate. This way you can do everything you need directly from your db instead of iterating over all the records in ruby:

Dato.group_by_month(:created_at).sum(:ingresos)

Here's where you can see the queries happening under the hood: https://github.com/ankane/groupdate/blob/master/lib/groupdate/relation_builder.rb.

Sum and Group by in Rails

This should work, assuming your model is called Person:

Person.group(:name).count(:widget_count)

You can find more info about it on RailsGuides for ActiveRecord.

Rails 5 group by, sum and show in view

You can calculate the combined totals of a category's sales by using ActiveRecord's #sum:

category = Category.last
category.sales.sum(:total)

To render the sales totals in your views, you can do something like this:

In the category model:

# app/models/category.rb
class Category < ApplicationRecord
def sales_total
sales.sum(:total)
end
end

In your relevant controller action:

# app/controllers/categories_controller.rb
@categories = Category.includes(:sales)

And in the corresponding view:

<% @categories.each do |c| %>
<%= "Category #{c.name}: Total $#{c.sales_total} %>
<% end %>

Hope this helps!



Related Topics



Leave a reply



Submit