File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,80 @@
1+
-- Problem:
2+
3+
/*
4+
Table: Prices
5+
6+
+---------------+---------+
7+
| Column Name | Type |
8+
+---------------+---------+
9+
| product_id | int |
10+
| start_date | date |
11+
| end_date | date |
12+
| price | int |
13+
+---------------+---------+
14+
(product_id, start_date, end_date) is the primary key (combination of columns with unique values) for this table.
15+
Each row of this table indicates the price of the product_id in the period from start_date to end_date.
16+
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.
17+
18+
19+
Table: UnitsSold
20+
21+
+---------------+---------+
22+
| Column Name | Type |
23+
+---------------+---------+
24+
| product_id | int |
25+
| purchase_date | date |
26+
| units | int |
27+
+---------------+---------+
28+
This table may contain duplicate rows.
29+
Each row of this table indicates the date, units, and product_id of each product sold.
30+
31+
32+
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.
33+
34+
Return the result table in any order.
35+
36+
The result format is in the following example.
37+
38+
39+
40+
Example 1:
41+
42+
Input:
43+
Prices table:
44+
+------------+------------+------------+--------+
45+
| product_id | start_date | end_date | price |
46+
+------------+------------+------------+--------+
47+
| 1 | 2019-02-17 | 2019-02-28 | 5 |
48+
| 1 | 2019-03-01 | 2019-03-22 | 20 |
49+
| 2 | 2019-02-01 | 2019-02-20 | 15 |
50+
| 2 | 2019-02-21 | 2019-03-31 | 30 |
51+
+------------+------------+------------+--------+
52+
UnitsSold table:
53+
+------------+---------------+-------+
54+
| product_id | purchase_date | units |
55+
+------------+---------------+-------+
56+
| 1 | 2019-02-25 | 100 |
57+
| 1 | 2019-03-01 | 15 |
58+
| 2 | 2019-02-10 | 200 |
59+
| 2 | 2019-03-22 | 30 |
60+
+------------+---------------+-------+
61+
Output:
62+
+------------+---------------+
63+
| product_id | average_price |
64+
+------------+---------------+
65+
| 1 | 6.96 |
66+
| 2 | 16.96 |
67+
+------------+---------------+
68+
Explanation:
69+
Average selling price = Total Price of Product / Number of products sold.
70+
Average selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96
71+
Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96
72+
*/
73+
74+
-------------------------------------------------------------------------------
75+
76+
-- Solution:
77+
178
SELECT p.product_id,ROUND(IFNULL(SUM(u.units * p.price)/SUM(u.units),0),2) AS average_price FROM Prices p
279
LEFT JOIN UnitsSold u ON p.product_id = u.product_id AND u.purchase_date BETWEEN p.start_date AND p.end_date
380
GROUP BY p.product_id

0 commit comments

Comments
 (0)