bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1251. Average Selling Price

1251. Average Selling Price

Leetcode Solutions

Calculating Average Selling Price Using SQL and Pandas

  1. Perform an SQL LEFT JOIN on Prices and UnitsSold tables based on product_id and ensure the purchase_date is within the start_date and end_date range.
  2. Calculate the total revenue for each product by multiplying the price by units sold.
  3. Group the results by product_id and sum both the total revenue and total units sold.
  4. Calculate the average selling price by dividing the total revenue by the total units sold for each product.
  5. Round the average selling price to two decimal places.
  6. Return the result set with product_id and average_price.
erDiagram
    Prices {
        int product_id
        date start_date
        date end_date
        int price
    }
    UnitsSold {
        int product_id
        date purchase_date
        int units
    }
    Prices ||--o{ UnitsSold : ""

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...