Den forespørgsel, der tæller for hver kundes samlede opkald og besvarede opkald for den angivne kampagne
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
Så kan du bruge forespørgslen ovenfor som en underforespørgsel til at bestille
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
Du kan inkludere alle ønskede kolonner i resultatforespørgslen