|
| 1 | +# [Average Selling Price](https://leetcode.com/problems/average-selling-price/description/?envType=study-plan-v2&envId=top-sql-50) |
| 2 | + |
| 3 | +### Problem Requirements: |
| 4 | + |
| 5 | +Find the average selling price for each product. <code>average_price</code> should be rounded to 2 decimal places. |
| 6 | + |
| 7 | +Return the result table in any order |
| 8 | + |
| 9 | +<details> |
| 10 | +<summary style="font-size:1.3rem;"> <strong>Hints</strong> </summary> |
| 11 | + |
| 12 | +<details> |
| 13 | + <summary>Hint#1</summary> |
| 14 | + <p>Use <code>OUTER JOIN</code></p> |
| 15 | +</details> |
| 16 | +<details> |
| 17 | + <summary>Hint#2</summary> |
| 18 | + <p>JOIN ON <code>product_id </code>AND <code>purchase_date</code></p> |
| 19 | +</details> |
| 20 | +<details> |
| 21 | + <summary>Hint#3</summary> |
| 22 | + <p>SQL has an aggregation function called <code>SUM(expression)</code> which calculate the sum of values in a set</p> |
| 23 | +</details> |
| 24 | +<details> |
| 25 | + <summary>Hint#4</summary> |
| 26 | + <p>SQL has a <code>ROUND(number, decimals)</code> function which rounds a number to a specified number of decimal places. </p> |
| 27 | +</details> |
| 28 | +<details> |
| 29 | + <summary>Hint#5</summary> |
| 30 | + <p>SQL has a <code>COALESCE(val1 , val2 , ....)</code> function which Return the first non-null value in a list </p> |
| 31 | +</details> |
| 32 | +<details> |
| 33 | + <summary>Hint#6</summary> |
| 34 | + <p>Use <code>GROUP BY</code> clause to group a set of rows into a set of summary rows</p> |
| 35 | +</details> |
| 36 | + |
| 37 | +</details> |
| 38 | + |
| 39 | +<details> |
| 40 | +<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary> |
| 41 | + |
| 42 | +To find the average_price for each product we want should calculate the total money paid and the total units bought then divide them as follows |
| 43 | + |
| 44 | + |
| 45 | +$$ |
| 46 | + round(\frac{SUM(price \times units)}{SUM(units)} , 2) |
| 47 | +$$ |
| 48 | + |
| 49 | +we will <code> JOIN</code> these tables <code> ON</code> <code> product_id </code> and <code> purchase_date</code>. The <code>purchase_date</code> should be between <code>start_date</code> and <code>end_date</code>. |
| 50 | +<ul> |
| 51 | + <li>To calculate the total money we can use <code>SUM()</code> function to sum the money of each record. The money of each record equals the <code>price</code>multiplied by <code>units</code>. </li> |
| 52 | + <li>To calculate the total units we also can use <code>SUM()</code> function </li>. |
| 53 | + <li>To calculate the <code>average_price</code> we should divide total money by total units then use <code>ROUND(number , decimals)</code> to round the result. </li> |
| 54 | +</ul> |
| 55 | +But what if the total units is zero here we will have <code>null</code>. To solve this problem we can use <code>COALESCE()</code> function. This function will return the first non-null value in a list. |
| 56 | +</details> |
| 57 | + |
| 58 | +<details> |
| 59 | +<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary> |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +```sql |
| 64 | +SELECT |
| 65 | + Prices.product_id, |
| 66 | + COALESCE( |
| 67 | + ROUND( |
| 68 | + SUM(Prices.price * UnitsSold.units) / SUM(UnitsSold.units), |
| 69 | + 2 |
| 70 | + ), |
| 71 | + 0 |
| 72 | + ) AS average_price |
| 73 | +FROM |
| 74 | + Prices |
| 75 | + LEFT OUTER JOIN UnitsSold ON Prices.product_id = UnitsSold.product_id |
| 76 | + AND UnitsSold.purchase_date >= Prices.start_date |
| 77 | + AND UnitsSold.purchase_date <= end_date |
| 78 | +GROUP BY |
| 79 | + product_id |
| 80 | +``` |
| 81 | +</details> |
0 commit comments