Skip to content
Onsite

Product price as of a given date

MediumP3Coding

Table price_changes:

ColumnType
product_idint
new_priceint
change_datedate

(product_id, change_date) is unique. Before its first change, every product costs 10.

Return the price of every product that appears in the table on 2024-03-15: the new_price of its latest change on or before that date, or 10 if all its changes are later.

Output columns: product_id, price.

Schema

CREATE TABLE price_changes (
  product_id int NOT NULL,
  new_price int NOT NULL,
  change_date date NOT NULL,
  PRIMARY KEY (product_id, change_date)
);

Example 1

Input
price_changes: (1, 20, 2024-03-01), (1, 30, 2024-03-20), (2, 50, 2024-03-20)
Output
(1, 20), (2, 10)

Constraints

  • PostgreSQL dialect.

Hints

PostgreSQL
Run checks the 3 visible tests, Submit all 6.