Price validity ranges

mediumData modelling~20 min · 4 tests · bitshop

price_changes is a change log: one row each time a product’s list price changed, effective from changed_at. To ask “what was the price on a given day?” you want each row to carry the range it was valid for. That’s a slowly changing dimension, type 2.

Return one row per row in price_changes with:

column type meaning
product_id integer the product
price decimal the list price
valid_from date the day this price took effect (changed_at)
valid_to date the day the next price for the same product took effect; NULL for the current price

Ranges are half-open: a price is valid on valid_from and every day up to, but not including, valid_to. So one row’s valid_to equals the next row’s valid_from, with no gap and no overlap.

Rows may have been inserted out of date order. Don’t rely on the table’s physical order.

Row order doesn’t matter.

Tables: price_changes.

solution.sqlDuckDB · bitshop

Press ⌘ ↵ or Run to check the visible tests. Submit runs the hidden ones too. Everything runs in your browser.

⌘ ↵ to run