[SQL]1251

1251. 平均售價

日期處於中間使用BETWEEN...AND...mysql

SELECT A.product_id, ROUND(SUM(A.price*A.units)/SUM(A.units),2) AS average_price
FROM (SELECT p.product_id, price, units
FROM Prices p LEFT JOIN UnitsSold u 
ON p.product_id = u.product_id
AND purchase_date BETWEEN start_date AND end_date) A
GROUP BY A.product_id;
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息