with t as( select o.product_id, o.quantity, o.unit_price, p.purchase_price from sales_orders o left join purchase_prices p on o.product_id=p.product_id where o.order_date like '2023%') select product_id, round(sum((unit_price-purchase_price)*quantity),2) as total_profit, round((avg(unit_price)-purchase_price)/purchase_price*100,2) as profit_margin from t group by product_id order by product_id