You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(product_id, start_date, end_date) is the primary key (combination of columns with unique values) for this table.
16
+
Each row of this table indicates the price of the product_id in the period from start_date to end_date.
17
+
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.
18
+
19
+
20
+
Table: UnitsSold
21
+
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
+
32
+
33
+
Write a solution to find the average selling price for each product. average_price should be rounded to 2 decimal places. If a product does not have any sold units, its average selling price is assumed to be 0.
34
+
35
+
Return the result table in any order.
36
+
*/
37
+
38
+
SELECT
39
+
p.product_id,
40
+
COALESCE(ROUND(SUM(p.price*u.units) *1.00/SUM(u.units), 2), 0.00) AS average_price
41
+
FROM Prices AS p
42
+
LEFT JOIN
43
+
UnitsSold AS u
44
+
ON
45
+
p.product_id=u.product_id
46
+
ANDu.purchase_date BETWEEN p.start_dateANDp.end_date
0 commit comments