Store the interval between changes
Stock is 14 units from Monday 09:00, 11 from Thursday 12:00, and 13 from Friday 08:00.
The interview prompt
Stock is 14 units from Monday 09:00, 11 from Thursday 12:00, and 13 from Friday 08:00. A report asks about Thursday at exactly noon. A Friday correction later says Thursday’s change actually took effect at 10:00.
Draw the boundary that two rows share
Write the intervals as [Monday 09:00, Thursday 12:00), [Thursday 12:00, Friday 08:00), and [Friday 08:00, open). The half-open convention assigns Thursday noon to 11. If both endpoints were inclusive, two balances would claim the same instant. Store the product and location on each interval so neighboring intervals are compared within the same stock position.
An as-of query finds the interval containing the requested instant. A duration-weighted average multiplies each quantity by the length of its overlap with the reporting window, then divides by the covered duration. Over Thursday 00:00–24:00, twelve hours at 14 and twelve hours at 11 average 12.5 units. The corrected threshold at 10:00 gives (10 × 14 + 14 × 11) / 24 = 12.25. Both calculations assume complete coverage for a 24-hour local day.
Apply the correction to both neighboring boundaries. Moving only the second interval’s start creates a two-hour overlap. If readers must reproduce the number they saw before Friday, retain recorded-time versions or the issued report as well.
The model behind the answer
A slow-selling title sits at 14 copies for six weeks. A daily snapshot writes 42 closing observations with the same quantity. If all inventory changes are captured, a timespan fact can instead record when the quantity of 14 began and when it ceased to apply. A change to 11 closes that interval and opens another.
For an as-of lookup, take the row whose start is at or before the requested instant and whose end is after it. Use an explicit convention for the open-ended interval. For a daily close, specify how events exactly at midnight fall into the business day. A unique start time alone cannot stop two intervals from overlapping.
The savings depend on how often stock changes and how people query it. A daily chart now needs a calendar expansion or interval join. I’d compare weeks of repeated rows saved against the measured cost of the most common reports. Kimball on timespan facts
Try it in SQLite
Move the inventory boundary
Shift the change from noon to 10:00. Inspect the quantity at the exact boundary and calculate the daily time-weighted mean.
Tables and sample data
Each run starts with fresh sample data. Use Ctrl+Enter or ⌘+Enter to run. Only your browser processes the query.
Review the two rows touched by a correction
Give the agent a migration that moves the Thursday stock change from noon to 10 a.m. Ask for the affected-row diff. Check the old interval’s end and the new interval’s start against the same instant.
Reviewer decision. Query one minute before 10, exactly at 10, and exactly at noon. The corrected quantities should be 14, 11 and 11, with one match apiece. Keep an original release if an analyst needs to reproduce the old answer. A blind overwrite destroys the evidence for that separate requirement.
Further reading. The Data Warehouse Toolkit, 3rd ed., on effective intervals in fact tables.
One more question
Inventory never changes, but the sensor stops reporting for a week. Is one uninterrupted interval still justified?
Check your reasoning
That depends on the evidence model. A complete change journal can justify carrying the last known balance. A periodically polled sensor with an outage establishes only its last observation. Track observation coverage separately or end the interval when the validity promise expires.
Previous · Find the dispatch that never happened · Next · Decide what a summary is allowed to forget · Complete series map