Practice Average Selling Price
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Tables: Prices(product_id INT, start_date DATE, end_date DATE, price INT); UnitsSold(product_id INT, purchase_date DATE, units INT). Prices has key (product_id, start_date, end_date); price periods never overlap for the same product. UnitsSold permits duplicate rows. Return product_id and average_price for every product. Match each sale to its inclusive price period, divide total price times units by total units, and round to two decimals. A product without sold units has average_price 0. Output order is unrestricted.
Source: Average Selling Price on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
Prices table: +------------+------------+------------+--------+ | product_id | start_date | end_date | price | +------------+------------+------------+--------+ | 1 | 2019-02-17 | 2019-02-28 | 5 | | 1 | 2019-03-01 | 2019-03-22 | 20 | | 2 | 2019-02-01 | 2019-02-20 | 15 | | 2 | 2019-02-21 | 2019-03-31 | 30 | +------------+------------+------------+--------+ UnitsSold table: +------------+---------------+-------+ | product_id | purchase_date | units | +------------+---------------+-------+ | 1 | 2019-02-25 | 100 | | 1 | 2019-03-01 | 15 | | 2 | 2019-02-10 | 200 | | 2 | 2019-03-22 | 30 | +------------+---------------+-------+
+------------+---------------+ | product_id | average_price | +------------+---------------+ | 1 | 6.96 | | 2 | 16.96 | +------------+---------------+
Related LeetCode Problems
Practice more problems similar to Average Selling Price.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.