日期處於中間使用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;