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) )
-> sqlfiddle
I provided an updated sqlfiddle for PostgreSQL. Your original seems to be for MySQL.
The tricks are:
generate_series()to create one row per day for every event.COALESCEto take care ofNULLvalues, which seem to be allowed forends_at.dailycost by dividing the price by the number of days betweenstarts_atandends_at+ 1to fix off-by-one.Default to 1 in case of
ends_at IS NULL.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).