|
| 1 | +<h2><a href="https://leetcode.com/problems/average-selling-price">Average Selling Price</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Table: <code>Prices</code></p> |
| 2 | + |
| 3 | +<pre> |
| 4 | ++---------------+---------+ |
| 5 | +| Column Name | Type | |
| 6 | ++---------------+---------+ |
| 7 | +| product_id | int | |
| 8 | +| start_date | date | |
| 9 | +| end_date | date | |
| 10 | +| price | int | |
| 11 | ++---------------+---------+ |
| 12 | +(product_id, start_date, end_date) is the primary key (combination of columns with unique values) for this table. |
| 13 | +Each row of this table indicates the price of the product_id in the period from start_date to end_date. |
| 14 | +For each product_id there will be no two overlapping periods. That means there will be no two intersecting periods for the same product_id. |
| 15 | +</pre> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | + |
| 19 | +<p>Table: <code>UnitsSold</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++---------------+---------+ |
| 23 | +| Column Name | Type | |
| 24 | ++---------------+---------+ |
| 25 | +| product_id | int | |
| 26 | +| purchase_date | date | |
| 27 | +| units | int | |
| 28 | ++---------------+---------+ |
| 29 | +This table may contain duplicate rows. |
| 30 | +Each row of this table indicates the date, units, and product_id of each product sold. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p> </p> |
| 34 | + |
| 35 | +<p>Write a solution to find the average selling price for each product. <code>average_price</code> should be <strong>rounded to 2 decimal places</strong>.</p> |
| 36 | + |
| 37 | +<p>Return the result table in <strong>any order</strong>.</p> |
| 38 | + |
| 39 | +<p>The result format is in the following example.</p> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong class="example">Example 1:</strong></p> |
| 43 | + |
| 44 | +<pre> |
| 45 | +<strong>Input:</strong> |
| 46 | +Prices table: |
| 47 | ++------------+------------+------------+--------+ |
| 48 | +| product_id | start_date | end_date | price | |
| 49 | ++------------+------------+------------+--------+ |
| 50 | +| 1 | 2019-02-17 | 2019-02-28 | 5 | |
| 51 | +| 1 | 2019-03-01 | 2019-03-22 | 20 | |
| 52 | +| 2 | 2019-02-01 | 2019-02-20 | 15 | |
| 53 | +| 2 | 2019-02-21 | 2019-03-31 | 30 | |
| 54 | ++------------+------------+------------+--------+ |
| 55 | +UnitsSold table: |
| 56 | ++------------+---------------+-------+ |
| 57 | +| product_id | purchase_date | units | |
| 58 | ++------------+---------------+-------+ |
| 59 | +| 1 | 2019-02-25 | 100 | |
| 60 | +| 1 | 2019-03-01 | 15 | |
| 61 | +| 2 | 2019-02-10 | 200 | |
| 62 | +| 2 | 2019-03-22 | 30 | |
| 63 | ++------------+---------------+-------+ |
| 64 | +<strong>Output:</strong> |
| 65 | ++------------+---------------+ |
| 66 | +| product_id | average_price | |
| 67 | ++------------+---------------+ |
| 68 | +| 1 | 6.96 | |
| 69 | +| 2 | 16.96 | |
| 70 | ++------------+---------------+ |
| 71 | +<strong>Explanation:</strong> |
| 72 | +Average selling price = Total Price of Product / Number of products sold. |
| 73 | +Average selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96 |
| 74 | +Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96 |
| 75 | +</pre> |
0 commit comments