List price at order time

hardData modelling~30 min · 4 tests · bitshop

Finance suspects that checkout charged the catalogue’s current price (products.price) instead of the price in effect when the order was placed. To check, they need every order line next to the list price that was valid on the order date.

The price history is in price_changes: one row per change, effective from changed_at (inclusive) until the product’s next change. A change on the order date itself applies to that order.

Return one row per row in order_items with:

column type meaning
order_id integer the order
product_id integer the product
order_date date from orders
list_price decimal the list price in effect on order_date
unit_price decimal what the line was charged
price_diff decimal unit_price - list_price, rounded to 2 decimals

Edge cases:

  • If no price was in effect yet on the order date (the order is older than the product’s first change, or the product has no history at all), keep the line with list_price and price_diff as NULL.
  • Price changes dated after the order don’t affect it.

Row order doesn’t matter.

Tables: order_items, orders, 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