SELECT p.name,SUM((tsl.unit_price_inc_tax-pl.purchase_price)*tsl.quantity) gross_profit,SUM(pl.purchase_price*pl.quantity) invested_capital,SUM((tsl.unit_price_inc_tax-pl.purchase_price)*tsl.quantity)-0.1*SUM(pl.purchase_price*pl.quantity) economic_profit FROM products p JOIN purchase_lines pl ON p.id=pl.product_id JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id WHERE t.type="sell" GROUP BY p.id,p.name ORDER BY economic_profit DESC;