#90. [LeetCode / MySQL] 1251. Average Selling Price

문제

 

입력 코드

1. 

SELECT 
    p.product_id,
    ROUND(SUM(units * price) / SUM(units), 2) AS average_price
FROM Prices AS p
JOIN UnitsSold AS u
    ON (p.product_id = u.product_id) 
        AND (u.purchase_date BETWEEN start_date AND end_date)
GROUP BY product_id

 

2. 

SELECT 
    p.product_id,
    ROUND(SUM(units * price) / SUM(units), 2) AS average_price
FROM Prices AS p
INNER JOIN UnitsSold AS u
    ON (p.product_id = u.product_id) 
WHERE u.purchase_date BETWEEN start_date AND end_date
GROUP BY product_id

 

코드 설명

#SELECT # ROUND #SUM #JOIN #INNER JOIN #BETWEEN #GROUP BY

 

 

문제 출처

 

Average Selling Price - LeetCode

Can you solve this real interview question? Average Selling Price - Table: Prices +---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | start_date | date | | end_date | date | | price | int | +---------------

leetcode.com