SELECT bl.name location,COUNT(t.id) visits,SUM((tsl.unit_price_inc_tax-tsl.unit_price_before_discount)*tsl.quantity) total_profit,SUM((tsl.unit_price_inc_tax-tsl.unit_price_before_discount)*tsl.quantity)/COUNT(t.id) profit_per_visit 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.id,bl.name ORDER BY profit_per_visit DESC;