Price history records a product price effective from an integer minute onward. For each order, select the latest price whose effective_minute is <= placed_minute. If multiple revisions share that effective minute, the largest revision_id wins. Future prices must never be used. Return order_id and total_cents (quantity times price), ordered by order_id. Keep orders without a historical price and return NULL for their total (an empty field in the output). Quantities are positive integers; prices are nonnegative integers.
Schema (all columns are NOT NULL unless explicitly nullable):
CREATE TABLE orders (order_id INTEGER PRIMARY KEY, product_id INTEGER NOT NULL, placed_minute INTEGER NOT NULL, quantity INTEGER NOT NULL);
CREATE TABLE prices (revision_id INTEGER PRIMARY KEY, product_id INTEGER NOT NULL, effective_minute INTEGER NOT NULL, price_cents INTEGER NOT NULL);Write one read-only SQLite query. The tables are already populated. Return exactly the requested columns and ordering, with no headers. Empty input produces no rows unless specified otherwise.
Example setup:
INSERT INTO orders VALUES (1,1,10,2),(2,1,20,1); INSERT INTO prices VALUES (1,1,0,100),(2,1,20,150);Expected output:
1|200
2|150Timebox: 9 minutes. Focus on correctness at boundaries and duplicate handling.
Sample tests