MS SQL using CTE
\nwith cte as (\n    select p.product_id, p.start_date, p.end_date, u.units as quantity, (p.price*u.units) as selling_price\n    from Prices p\n    left join Un
```\nwith cte as (\n    select p.product_id, p.start_date, p.end_date, u.units as quantity, (p.price*u.units) as selling_price\n    from Prices p\n    left join UnitsSold u\n    on p.product_id = u.product_id\n    and u.purchase_date between p.start_date and p.end_date\n)\n\nselect product_id, round ((sum(selling_price) * 1.00 /sum(quantity)),2) as average_price\nfrom cte\ngroup by product_id\n```
[]