Really a simple solution
\n# Code\n\n/* Write your T-SQL query statement below */\nSELECT\n    P.product_id,\n    ISNULL(ROUND(SUM(P.price * US.units * 1.0) / SUM(US.units), 2), 0) AS a
\n# Code\n```\n/* Write your T-SQL query statement below */\nSELECT\n    P.product_id,\n    ISNULL(ROUND(SUM(P.price * US.units * 1.0) / SUM(US.units), 2), 0) AS average_price \nFROM Prices P\n    LEFT LOOP JOIN UnitsSold US ON US.product_id = P.product_id \n        AND US.purchase_date BETWEEN P.start_date AND P.end_date\nGROUP BY P.product_id       \n            \n```
['MS SQL Server']