MS SQL Solution Beats 85.60%. Left join, between, isnull, round, sum
Code\n\n/* Write your T-SQL query statement below */\nselect Prices.product_id, \nisnull(round(sum(Prices.price * UnitsSold.units * 1.0) / sum(UnitsSold.units),
# Code\n```\n/* Write your T-SQL query statement below */\nselect Prices.product_id, \nisnull(round(sum(Prices.price * UnitsSold.units * 1.0) / sum(UnitsSold.units),2), 0)\nas average_price\nfrom Prices left join UnitsSold\non Prices.product_id = UnitsSold.product_id\nand UnitsSold.purchase_date between start_date and Prices.end_date\ngroup by Prices.product_id\n```\n\n![2024-07-23 17.29.43.jpg](https://assets.leetcode.com/users/images/533705be-a2a0-44cb-9f1d-749bd3c3811d_1722167486.3343527.jpeg)\n
['MS SQL Server']