SELECT bl.state,bl.city,COUNT(t.id) transactions,SUM(t.final_total) revenue,AVG((tsl.unit_price_inc_tax-tsl.unit_price_before_discount)/tsl.unit_price_before_discount)*100 avg_margin FROM business_locations bl JOIN transactions t ON bl.id=t.location_id JOIN transaction_sell_lines tsl ON t.id=tsl.transaction_id WHERE t.type="sell" GROUP BY bl.state,bl.city ORDER BY avg_margin DESC;