MySQL Properly Styled, SUM, LEFT JOIN, BETWEEN
Once unspoken requirement that you need to understand is that the purchase price applies to the date of purchase.\nEx. I made a purchase on 1-1-2021 buying 10 u
Once unspoken requirement that you need to understand is that the purchase price applies to the date of purchase.\nEx. I made a purchase on 1-1-2021 buying 10 units of product 3. This price is $4.00 between 12-1-2020 and 1-31-2021.\n\n```\nSELECT\n    Prices.product_id AS product_id,\n    ROUND(SUM(Prices.price * UnitsSold.units) / SUM(UnitsSold.units), 2) AS average_price\nFROM Prices\nLEFT JOIN UnitsSold\n    ON Prices.product_id = UnitsSold.product_id\n        AND UnitsSold.purchase_date BETWEEN Prices.start_date AND Prices.end_date\nGROUP BY Prices.product_id\n```\n\n\n
[]