Skip to content

Commit 9374271

Browse files
authored
Merge pull request #86 from remy727/1251-average-selling-price
1251. Average Selling Price
2 parents acc5035 + 3a041c0 commit 9374271

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@
385385
| 620 | Not Boring Movies | [MySQL](./database/0620-not-boring-movies.sql) | Easy |
386386
| 1068 | Product Sales Analysis I | [Ruby](./database/1068-product-sales-analysis-i.sql) | Easy |
387387
| 1148 | Article Views I | [MySQL](./database/1148-article-views-i.sql) | Easy |
388+
| 1251 | Average Selling Price | [MySQL](./database/1251-average-selling-price.sql) | Easy |
388389
| 1280 | Students and Examinations | [MySQL](./database/1280-students-and-examinations.sql) | Easy |
389390
| 1378 | Replace Employee ID With The Unique Identifier | [MySQL](./database/1378-replace-employee-id-with-the-unique-identifier.sql) | Easy |
390391
| 1581 | Customer Who Visited but Did Not Make Any Transactions | [MySQL](./database/1581-customer-who-visited-but-did-not-make-any-transactions.sql) | Easy |
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
-- 1251. Average Selling Price
2+
-- Easy
3+
-- https://leetcode.com/problems/average-selling-price
4+
5+
/*
6+
Table: Prices
7+
+---------------+---------+
8+
| Column Name | Type |
9+
+---------------+---------+
10+
| product_id | int |
11+
| start_date | date |
12+
| end_date | date |
13+
| price | int |
14+
+---------------+---------+
15+
(product_id, start_date, end_date) is the primary key 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+
Table: UnitsSold
20+
+---------------+---------+
21+
| Column Name | Type |
22+
+---------------+---------+
23+
| product_id | int |
24+
| purchase_date | date |
25+
| units | int |
26+
+---------------+---------+
27+
There is no primary key for this table, it may contain duplicates.
28+
Each row of this table indicates the date, units, and product_id of each product sold.
29+
30+
Write an SQL query to find the average selling price for each product. average_price should be rounded to 2 decimal places.
31+
Return the result table in any order.
32+
The query result format is in the following example.
33+
34+
Example 1:
35+
Input:
36+
Prices table:
37+
+------------+------------+------------+--------+
38+
| product_id | start_date | end_date | price |
39+
+------------+------------+------------+--------+
40+
| 1 | 2019-02-17 | 2019-02-28 | 5 |
41+
| 1 | 2019-03-01 | 2019-03-22 | 20 |
42+
| 2 | 2019-02-01 | 2019-02-20 | 15 |
43+
| 2 | 2019-02-21 | 2019-03-31 | 30 |
44+
+------------+------------+------------+--------+
45+
UnitsSold table:
46+
+------------+---------------+-------+
47+
| product_id | purchase_date | units |
48+
+------------+---------------+-------+
49+
| 1 | 2019-02-25 | 100 |
50+
| 1 | 2019-03-01 | 15 |
51+
| 2 | 2019-02-10 | 200 |
52+
| 2 | 2019-03-22 | 30 |
53+
+------------+---------------+-------+
54+
Output:
55+
+------------+---------------+
56+
| product_id | average_price |
57+
+------------+---------------+
58+
| 1 | 6.96 |
59+
| 2 | 16.96 |
60+
+------------+---------------+
61+
Explanation:
62+
Average selling price = Total Price of Product / Number of products sold.
63+
Average selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96
64+
Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96
65+
*/
66+
67+
SELECT
68+
UnitsSold.product_id,
69+
ROUND(SUM(UnitsSold.units * Prices.price) / SUM(UnitsSold.units), 2) AS average_price
70+
FROM
71+
UnitsSold
72+
JOIN
73+
Prices
74+
ON
75+
UnitsSold.product_id = Prices.product_id
76+
AND UnitsSold.purchase_date BETWEEN Prices.start_date AND Prices.end_date
77+
GROUP BY
78+
UnitsSold.product_id;

0 commit comments

Comments
 (0)