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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:01:30+00:00 2026-06-17T04:01:30+00:00

I have this Event model class Event < ActiveRecord::Base attr_accessible :starts_at, :ends_at, :price end

  • 0

I have this Event model

class Event < ActiveRecord::Base
  attr_accessible :starts_at, :ends_at, :price
end

An Event can be a single instant event:

Event.create(:starts_at => Date.today, :ends_at=>nil, :price => 10.0)

It can also span multiple days or months:

Event.create(:starts_at => Date.today, :ends_at => (Date.today + 2.months), :price => 20.0)

I want to break down the cost of events by months, so that cost for an instant event falls into the month it belongs to, naturally, and cost for an event that spans multiple months should be divided proportionally between those months.

Obviously this would be difficult to handle using SQL, but maybe someone has some advice on that?

What would be the most efficient way of calculating this aggregation?

Update:

Here is a clearly defined structure for the data on sqlfiddle: http://sqlfiddle.com/#!12/a532e/6

What I would want from this dataset is something like:

( (Date('2013-01-01'), 31.6), (Date('2013-02-01'), 8.4) )
  • 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-17T04:01:32+00:00Added an answer on June 17, 2026 at 4:01 am
    SELECT event_id
          ,date_trunc('month', day)::date AS month
          ,sum(daily) AS price_this_month
    FROM  (
       SELECT event_id
             ,generate_series(starts_at
                             ,COALESCE(ends_at, starts_at)
                             ,interval '1 day') AS day
             ,price / COALESCE((ends_at - starts_at) + 1, 1) AS daily
       FROM   events
       ) a
    GROUP  BY 1,2
    ORDER  BY 1,2;
    

    -> sqlfiddle

    I provided an updated sqlfiddle for PostgreSQL. Your original seems to be for MySQL.

    The tricks are:

    • Use generate_series() to create one row per day for every event.
    • Use COALESCE to take care of NULL values, which seem to be allowed for ends_at.
    • Calculate the daily cost by dividing the price by the number of days between starts_at and ends_at
      + 1 to fix off-by-one.
      Default to 1 in case of ends_at IS NULL.
    • Re-aggregate per event and month.

    Voilá.

    You even get exact rates per month, depending on how many days a month has. February (28 days) is cheaper than January (31 days).

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

Sidebar

Related Questions

Hi have a model like this: class EventDate < ActiveRecord::Base belongs_to :event named_scope :named,
I have this code in my user model: class User < ActiveRecord::Base attr_accessible :email,
I have the following models: class EventParticipant < ActiveRecord::Base belongs_to :event has_one :user attr_accessible
I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have the following (simplified) model and migration: Model: class User < ActiveRecord::Base attr_readonly
I have three models: class Coupon < ActiveRecord::Base belongs_to :event has_many :coupon_events, :dependent =>
I have an Account model and a User model: class Account < ActiveRecord::Base has_many
This is in Ruby 1.9.3p194, with Rails 3.2.8 app/models/owner.rb: class Owner < ActiveRecord::Base attr_accessible
Here are my models: class BillingProfile < ActiveRecord::Base belongs_to :student attr_accessible :cost end class
having this code block of an example rails model class: class Block < ActiveRecord::Base

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.