SQL Server using window function faster than 97.18%
select distinct\n    p.product_id,\n    cast(cast(sum(price*units) over (partition by p.product_id) as numeric)/cast(sum(units) over(partition by  p.product_id)
select distinct\n    p.product_id,\n    cast(cast(sum(price*units) over (partition by p.product_id) as numeric)/cast(sum(units) over(partition by  p.product_id) as numeric) as decimal(10,2)) as average_price\nfrom Prices p\njoin UnitsSold s\non p.product_id = s.product_id and\n    s.purchase_date between p.start_date and p.end_date
[]