LeetCode-1251. The average selling price (simple)

Table: Prices

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| product_id | int |
| start_date | DATE |
| END_DATE | DATE |
|. price | int |
+ --------------- + --- + ------
(the product_id, START_DATE, END_DATE) Prices table is the primary key.
Prices for each row of the table represents a certain price within a period of time.
Corresponding to the time period of each product will not overlap, which means will not cross the same period the price of a product.
 

Table: UnitsSold

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| product_id | int |
| PURCHASE_DATE | DATE |
| Units | int |
+ --------------- + -------- - +
UnitsSold no primary key table, it may contain duplicate entries.
Each row UnitsSold table indicates the date for each product sold, units and product id.
 

Write SQL queries to find the average selling price of each product.
average_price should be rounded to two decimal places.
Results illustrated in the following format:

Prices table:
+------------+------------+------------+--------+
| product_id | start_date | end_date   | price  |
+------------+------------+------------+--------+
| 1          | 2019-02-17 | 2019-02-28 | 5      |
| 1          | 2019-03-01 | 2019-03-22 | 20     |
| 2          | 2019-02-01 | 2019-02-20 | 15     |
| 2          | 2019-02-21 | 2019-03-31 | 30     |
+------------+------------+------------+--------+
 
UnitsSold table:
+------------+---------------+-------+
| product_id | purchase_date | units |
+------------+---------------+-------+
| 1          | 2019-02-25    | 100   |
| 1          | 2019-03-01    | 15    |
| 2          | 2019-02-10    | 200   |
| 2          | 2019-03-22    | 30    |
+------------+---------------+-------+

The Table the Result:
+ ------------ + --------------- +
| product_id | Average_Price |
+ ----------- - + --------------- +
| 1 | 6.96 |
| 2 | 16.96 |
+ ------------ + -------- ------- +
average product price = total price / sales number of products.
The product of the average price = ((5 * 100) + (15 * 20)) / 115 = 6.96
The average selling prices 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/average-selling-price
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation: write SQL queries to find the average selling price of each product.

Thoughts: The average selling price = total number of products product / sales.

Product price multiplied by the number equal to the total price,

Problem solving:

select product_id,round(sum(a)/sum(units),2) as average_price from(
    select 
        p.product_id as product_id,
        price,units,
        price*units as a
    from Prices p 
    left join UnitsSold u
    on p.product_id=u.product_id and (purchase_date<=end_date and purchase_date>=start_date))t
group by product_id

Method Two:

--  Write your MySQL query statement below

SELECT a.product_id
	, round(SUM(a.units * b.price) / SUM(a.units), 2) AS average_price
FROM UnitsSold a
	JOIN Prices b
	ON (a.product_id = b.product_id
		AND a.purchase_date >= b.start_date
		AND a.purchase_date <= b.end_date)
GROUP BY product_id

Knowledge points:

Published 144 original articles · won praise 2 · Views 5741

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104749332