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 9087783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:46:34+00:00 2026-06-16T21:46:34+00:00

I have Rails app with a PostgreSQL DB Sales Table that looks like this:

  • 0

I have Rails app with a PostgreSQL DB Sales Table that looks like this:

              Sales
price(decimal)   date(datetime)
100.20           11/25/2012 11:00:00
30.43            11/26/2012 03:00:00
124.43           11/25/2012 18:00:00
190.34           11/26/2012 22:00:00

The DB has thousands of records like this, spanning 10 years.

I need to find the average sales price for each day and save each as a separate record in another DB table via a rake task.

For further clarity, Sales need to be grouped by day (even though data is in datetime format), and then the price values for that day need to be averaged.

Since I have 10 years of days, the ability to loop through each daily group and perform the averages is essential.

Does anyone have any suggestions as to how to write this?

  • 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-06-16T21:46:35+00:00Added an answer on June 16, 2026 at 9:46 pm

    You’ll need to double check the syntax, but something like this should do it all in the database…

    Sales.average(:price, :group => "DATE_TRUNC('day', date)")
    

    Note that DATE_TRUNC is PG specific.

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

Sidebar

Related Questions

I have a rails app with a PostgreSQL database with hourly price data for
I have a DB table for my Rails 3.2.8 app as a PostgreSQL database.
I have a Ruby on Rails app with a PostgreSQL database which has this
I have a rails app that requires multi-table inheritance for which I am using
I have a Ruby/Rails app. I have an artists table in my postgresql database
I have a Rails app that is served by Apache and Passenger, using only
I have a rails app, where Im having a drop box like feature-set. Each
I have a Rails app that does everything I need it to do via
In my ruby-on-rails app, I have nested comments that can be nested an arbitrary
In my Rails app, I have the following PostgreSQL query to find the most

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.