SOLUTION WITH SUM() OVER() ( SQL SERVER )
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\n\r\n# Approach\r\n Describe your approach to solving the problem. \r\n\r\n# Complex
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n\r\n# Complexity\r\n- Time complexity:\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity:\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```\r\n/* Write your T-SQL query statement below */\r\n\r\n\r\n\r\nWITH CTE AS (\r\n        SELECT\r\n            T1.product_id,\r\n            T1.price,\r\n            T2.units,\r\n            T2.units * CAST(T1.price AS DECIMAL(10,2)) [selling]\r\n        FROM    \r\n            Prices T1 \r\n        LEFT JOIN\r\n            UnitsSold T2 ON T1.product_id = T2.product_id\r\n            AND\r\n                T2.purchase_date BETWEEN T1.start_date AND T1.end_date\r\n)\r\nSELECT DISTINCT\r\n    product_id,\r\n    ROUND(SUM([selling]) OVER(PARTITION BY product_id) / CAST(SUM(units) OVER(PARTITION BY product_id) AS DECIMAL(10,2)),2) average_price\r\nFROM\r\n    CTE\r\n```
['MS SQL Server']