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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:57:20+00:00 2026-06-16T17:57:20+00:00

I have a rails app with a PostgreSQL database with hourly price data for

  • 0

I have a rails app with a PostgreSQL database with hourly price data for the last 10 years, and I need to get the daily average price for each day saved in a new DB table. This query groups records by day and returns an average price for each day:

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

The Rails Console response looks like this (a sample of 2 days of data):

{"2013-01-03 00:00:00"=>#<BigDecimal:7fcc7c2e4a28,'0.1752888888 88888889E2',27(27)>, "2013-01-02 00:00:00"=>#<BigDecimal:7fcc7c2e4848,'0.2547086956 52173913E2',27(27)>}

Can anyone suggest how to write some code to save these returned date and averageprice values to a new Object called DailyAverage?

  • 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-16T17:57:21+00:00Added an answer on June 16, 2026 at 5:57 pm

    Your response is like:

    {
      'date' => BigDecimal,
      'date2' => BigDecimal,
      'date2' => BigDecimal,
    }
    

    So you can access to each average for each date like this:

    averages = Sale.average(:price, :group => "DATE_TRUNC('day', date)")
    
    averages.each do |date, avg|
      DailyAverage.create(date: date, average: avg.to_f)
    end
    

    This code assumes you have a DailyAverage model with date and average attributes.

    You may have to use a to_date in the each loop:

    averages.each do |date, avg|
      DailyAverage.create(date: date.to_date, average: avg.to_f)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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 Ruby/Rails app. I have an artists table in my postgresql database
I have a Rails 3 app deployed on Appfog with a postgresql database. I
I have a Rails app running Mongoid on Heroku and I need to set
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 Rails app, I have the following PostgreSQL query to find the most
I have Rails app with a Postgres backend. I need to add full text
I have a Rails app with Users, and each user HABTM Roles. I want

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.