SQL SERVER NEW TEST CASES ADDED/SOLVED WITH BEATS %90 557 ms
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nIt is one of the classi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIt is one of the classic aggregate function, group by and join combine problems. Main issue is that null value if the product has a price but not sold. You have to make arrangement in the aggregate function. \nAggregate Funciton: Inside of this calculation i am using mathematical represent rather than AVG() built-in function. If units in the numerator part is NULL than we change it to 0 AND also units in denominator part we change the 1 Because 1 is the ineffective element in the partition. \nIn addition to that you have to do LEFT or RIGHT join (depends on your writing) to get the products only in prices table.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nSELECT p.product_id,\n\t\tROUND(SUM(price * ISNULL(units,0))*1.00 / ISNULL(SUM(units),1),2) AS average_price FROM prices p\nLEFT JOIN UnitsSold u ON u.product_id = p.product_id AND purchase_date BETWEEN start_date AND end_date\nGROUP BY p.product_id\n\n```
['MS SQL Server']