SELECT bl.name location,COUNT(DISTINCT t.contact_id) total_customers,COUNT(DISTINCT CASE WHEN c.total_rp>0 THEN t.contact_id END) loyalty_customers FROM business_locations bl JOIN transactions t ON bl.id=t.location_id JOIN contacts c ON t.contact_id=c.id WHERE t.type="sell" GROUP BY bl.id,bl.name ORDER BY loyalty_customers DESC;