Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3635692
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:56:10+00:00 2026-05-19T00:56:10+00:00

How does one sum all of the total columns in an association? My SQL-fu

  • 0

How does one sum all of the “total” columns in an association?

My SQL-fu sucks, so I would like to learn how to do so using Active Record for my rails 2.3.5 app (so no fancy syntax just yet please 😉 And I’m on MySQL.

Let’s say I have:


Shop 

has_many :customers
has_many :transactions, :through => :customers

So normal stuff.


shop = Shop.first
shop.transactions
=> 100 

Ok, all that background for the question:

I want to SUM the total column in the transactions from the past year (Jan 1st 2010..Dec 31 2010) and display them by customer.

Whilst I know how to group transactions and find with conditions, it’s the sum part where my lack of SQL lets me down.


first = Date.new(2010, 01, 01)
last = Date.new(2010, 12, 31)

shop.transactions(:conditions => {:created_at => first..last}, :group => :customer_id, :include => sum(:total))

I just took a stab, am i on the right track?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:56:10+00:00Added an answer on May 19, 2026 at 12:56 am
    
    shop.transactions.sum(:total, :conditions => {:created_at => first..last}, :group => :customer_id)
    

    This looks like an easier way. I now know that sum can take AR attributes too. cool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a related question to one I posted earlier... I'm trying to sum
Say I have a Customer - CustomerOrder one-to-many bi-directional relationship with the CustomerOrder holding
I am trying to create a query that allows me to get the sum
I've got models like this: class PledgeItem(models.Model): title = models.CharField(...) usd_amount = models.DecimalField(...) class
I'm trying to do a rather complicated SELECT computation that I will generalize: Main
I have a products table, with the fields product, category and cost, of type
In a form I have, a consumer can choose to add a blood test,
I've been reading about Python's multiprocessing module . I still don't think I have
I have a field in a table which contains bitwise flags. Let's say for
As I continue my quest of learning functional programming, I've come to wonder if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.