[SQL]1251

1251. 平均售价

日期处于中间使用BETWEEN...AND...

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;
posted @ 2020-04-06 22:14  whiky  阅读(182)  评论(0编辑  收藏  举报