본문 바로가기
SQL

leetcode 1251. Average Selling Price

by 무적김두칠 2023. 4. 7.

https://leetcode.com/problems/average-selling-price/description/

 

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

 

1
2
3
4
5
6
7
# Write your MySQL query statement below
select p.product_id,round(sum(p.price*u.units)/sum(u.units),2) average_price 
from Prices p
join UnitsSold u
on p.product_id = u.product_id
and u.purchase_date between p.start_date and p.end_date
group by p.product_id
cs
반응형

댓글