본문 바로가기
취준/코팅 테스트

[LeetCode] - 1251. Average Selling Price

by 소복소복뚜벅뚜벅 2024. 3. 28.

문제 

Write a solution to find the average selling price for each product. average_price should be rounded to 2 decimal places.

Return the result table in any order.

The result format is in the following example.

 

문제 풀이 

SELECT p.product_id
	, IFNULL(ROUND(SUM(units*price) / SUM(units),2),0) AS 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

 

오답 이유 

1. 순간적으로 JOIN을 할 때, 날짜 사이에 값을 배치하는 법을 까먹었다.