This repository was archived by the owner on Apr 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1251.sql
More file actions
55 lines (42 loc) · 1.43 KB
/
Copy path1251.sql
File metadata and controls
55 lines (42 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
USE leetcode;
# Tables: Prices, UnitsSold
CREATE TABLE IF NOT EXISTS Prices
(
product_id INT,
start_date DATE,
end_date DATE,
price INT
);
CREATE TABLE IF NOT EXISTS UnitsSold
(
product_id INT,
purchase_date DATE,
units INT
);
TRUNCATE TABLE Prices;
INSERT INTO Prices (product_id, start_date, end_date, price)
VALUES ('1', '2019-02-17', '2019-02-28', '5');
INSERT INTO Prices (product_id, start_date, end_date, price)
VALUES ('1', '2019-03-01', '2019-03-22', '20');
INSERT INTO Prices (product_id, start_date, end_date, price)
VALUES ('2', '2019-02-01', '2019-02-20', '15');
INSERT INTO Prices (product_id, start_date, end_date, price)
VALUES ('2', '2019-02-21', '2019-03-31', '30');
TRUNCATE TABLE UnitsSold;
INSERT INTO UnitsSold (product_id, purchase_date, units)
VALUES ('1', '2019-02-25', '100');
INSERT INTO UnitsSold (product_id, purchase_date, units)
VALUES ('1', '2019-03-01', '15');
INSERT INTO UnitsSold (product_id, purchase_date, units)
VALUES ('2', '2019-02-10', '200');
INSERT INTO UnitsSold (product_id, purchase_date, units)
VALUES ('2', '2019-03-22', '30');
# Solution
SELECT p.product_id,
IFNULL(ROUND(SUM(u.units * p.price) / SUM(u.units), 2), 0) average_price
FROM Prices p
LEFT JOIN UnitsSold u
ON p.product_id = u.product_id
AND u.purchase_date BETWEEN start_date
AND end_date
GROUP BY p.product_id;