diff --git a/solution/1200-1299/1251.Average Selling Price/README.md b/solution/1200-1299/1251.Average Selling Price/README.md index 01da77b705cf9..58915e7761c37 100644 --- a/solution/1200-1299/1251.Average Selling Price/README.md +++ b/solution/1200-1299/1251.Average Selling Price/README.md @@ -83,7 +83,14 @@ Result table: ### **SQL** ```sql - +SELECT p.product_id, + Round(( Sum(u.units * p.price) + 0.0 ) / ( Sum(units) + 0.0 ), 2) + average_price +FROM Prices p + INNER JOIN UnitsSold u + ON p.product_id = u.product_id +WHERE u.purchase_date BETWEEN p.start_date AND p.end_date +GROUP BY p.product_id; ``` diff --git a/solution/1200-1299/1251.Average Selling Price/README_EN.md b/solution/1200-1299/1251.Average Selling Price/README_EN.md index c419fa7c33666..06eb6495c638d 100644 --- a/solution/1200-1299/1251.Average Selling Price/README_EN.md +++ b/solution/1200-1299/1251.Average Selling Price/README_EN.md @@ -87,7 +87,14 @@ Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96 ### **SQL** ```sql - +SELECT p.product_id, + Round(( Sum(u.units * p.price) + 0.0 ) / ( Sum(units) + 0.0 ), 2) + average_price +FROM Prices p + INNER JOIN UnitsSold u + ON p.product_id = u.product_id +WHERE u.purchase_date BETWEEN p.start_date AND p.end_date +GROUP BY p.product_id; ``` diff --git a/solution/1200-1299/1251.Average Selling Price/Solution.sql b/solution/1200-1299/1251.Average Selling Price/Solution.sql new file mode 100644 index 0000000000000..dda9eb5474f51 --- /dev/null +++ b/solution/1200-1299/1251.Average Selling Price/Solution.sql @@ -0,0 +1,8 @@ +SELECT p.product_id, + Round(( Sum(u.units * p.price) + 0.0 ) / ( Sum(units) + 0.0 ), 2) + average_price +FROM Prices p + INNER JOIN UnitsSold u + ON p.product_id = u.product_id +WHERE u.purchase_date BETWEEN p.start_date AND p.end_date +GROUP BY p.product_id;