Skip to content

Commit d8c7050

Browse files
refactor 1251
1 parent e89e904 commit d8c7050

File tree

1 file changed

+1
-69
lines changed

1 file changed

+1
-69
lines changed

database/_1251.sql

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,7 @@
1-
--1251. Average Selling Price
2-
--
3-
--Table: Prices
4-
--
5-
--+---------------+---------+
6-
--| Column Name | Type |
7-
--+---------------+---------+
8-
--| product_id | int |
9-
--| start_date | date |
10-
--| end_date | date |
11-
--| price | int |
12-
--+---------------+---------+
13-
--(product_id, start_date, end_date) is the primary key for this table.
14-
--Each row of this table indicates the price of the product_id in the period from start_date to end_date.
15-
--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.
16-
--
17-
--
18-
--Table: UnitsSold
19-
--
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-
--
31-
--Write an SQL query to find the average selling price for each product.
32-
--
33-
--average_price should be rounded to 2 decimal places.
34-
--
35-
--The query result format is in the following example:
36-
--
37-
--Prices table:
38-
--+------------+------------+------------+--------+
39-
--| product_id | start_date | end_date | price |
40-
--+------------+------------+------------+--------+
41-
--| 1 | 2019-02-17 | 2019-02-28 | 5 |
42-
--| 1 | 2019-03-01 | 2019-03-22 | 20 |
43-
--| 2 | 2019-02-01 | 2019-02-20 | 15 |
44-
--| 2 | 2019-02-21 | 2019-03-31 | 30 |
45-
--+------------+------------+------------+--------+
46-
--
47-
--UnitsSold table:
48-
--+------------+---------------+-------+
49-
--| product_id | purchase_date | units |
50-
--+------------+---------------+-------+
51-
--| 1 | 2019-02-25 | 100 |
52-
--| 1 | 2019-03-01 | 15 |
53-
--| 2 | 2019-02-10 | 200 |
54-
--| 2 | 2019-03-22 | 30 |
55-
--+------------+---------------+-------+
56-
--
57-
--Result table:
58-
--+------------+---------------+
59-
--| product_id | average_price |
60-
--+------------+---------------+
61-
--| 1 | 6.96 |
62-
--| 2 | 16.96 |
63-
--+------------+---------------+
64-
--Average selling price = Total Price of Product / Number of products sold.
65-
--Average selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96
66-
--Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96
67-
681
select a.product_id, round(
692
sum(a.price * b.units)/sum(b.units),
703
2) as average_price
714
from Prices as a
725
join UnitsSold as b
736
on a.product_id = b.product_id and (b.purchase_date between a.start_date and a.end_date)
74-
group by a.product_id;
75-
7+
group by a.product_id;

0 commit comments

Comments
 (0)