La query che conta per le chiamate totali di ciascun cliente e le chiamate con risposta per la campagna specificata
select
c.id,
count(*) as total_calls,
sum(case when answered=1 then 1 else 0 end) as answered_calls
from customer c
join calls cs on c.id=cs.customer_id
where cs.campaign='2016-09'
group by c.id
Quindi puoi utilizzare la query sopra come sottoquery per ordinare
select sub.id, (@rank:[email protected]+1) as rank
from (the subquery above) sub, (select @rank:=1)
order by
case when sub.total_calls=0 then 0 else 1,
sub.total_calls,
sub.answered_calls*100/sub.total_calls
Puoi includere qualsiasi colonna desiderata nella query dei risultati