MySQL - Count call answered within certain ranges 0 to 10 sec, group by count -
i need count call answered within ranges 0 10 sec, 0 20 sec, etc. count increase while delta different count between current , next. % current count / final count total.
here sqlfiddle can use data testing: http://sqlfiddle.com/#!9/803d2/2
sample table of callsdetails:
+-----+----------------+----------+----------+---------------+ | id | callid | callerno | duration | status | +-----+----------------+----------+----------+---------------+ | 634 | 1479097551.228 | 1000 | 2 | complete | | 635 | 1479102518.248 | 1000 | 12 | complete | +-----+----------------+----------+----------+---------------+ expected result:
+------------------------+----------+----------+----------+ | ranges | count | delta | % | +------------------------+----------+----------+----------+ | between 0 10 secs | 44 | +44 | 84.62 % | | between 0 20 secs | 48 | +4 | 92.31 % | | between 0 30 secs | 50 | +2 | 96.15 % | | between 0 40 secs | 51 | +1 | 98.08 % | | between 0 50 secs | 51 | +0 | 98.08 % | | between 0 60 secs | 51 | +0 | 98.08 % | | between 0 70 secs | 51 | +0 | 98.08 % | | between 0 80 secs | 52 | +1 | 100.00 % | | between 0 90 secs | 52 | +0 | 100.00 % | | between 0 100+ secs | 52 | +0 | 100.00 % | +------------------------+----------+----------+----------+ total 52 what able create below query, if can provide better solution, please advice. problem face (priority) not able count , (secondary) final count total (52) %, manually put in final count total (52). please help.
select ranges,delta,round(delta/52*100,2) '%' ( select ( if(duration<=10,'10',if(duration<=20,'20',if(duration<=30,'30', if(duration<=40,'40',if(duration<=50,'50', if(duration<=60,'60',if(duration<=70,'70',if(duration<=80, '80',if(duration<=90,'90','100+')))))))))) ranges,count(duration) delta callsdetails group ranges ) group ranges; current result:
+--------+-------+-------+ | ranges | delta | % | +--------+-------+-------+ | 10 | 44 | 84.62 | | 20 | 4 | 7.69 | | 30 | 2 | 3.85 | | 40 | 1 | 1.92 | | 80 | 1 | 1.92 | +--------+-------+-------+
one way (using variable obtain cumulative sum, , using join intervals. @ last added join select count(*) obtain 52):
select ran, tot_count, delta, round(tot_count/e.tot*100,2) '%' (select b.ran, a.ranges, coalesce(a.delta,0) delta , @r:=@r+coalesce(a.delta,0) tot_count (select ( if(duration<=10,'10',if(duration<=20,'20',if(duration<=30,'30', if(duration<=40,'40',if(duration<=50,'50', if(duration<=60,'60',if(duration<=70,'70',if(duration<=80, '80',if(duration<=90,'90','100+')))))))))) ranges,count(duration) delta callsdetails c group ranges) right join (select '10' ran, 1 ord union select '20', 2 ord union select '30', 3 union select '40', 4 union select '50', 5 union select '60',6 union select '70',7 union select '80',8 union select '90',9 union select '100+',10) b on a.ranges=b.ran cross join (select @r:=0) t order ord ) d cross join (select count(*) tot callsdetails) e ; output:
ran tot_count delta % 10 44 44 84.62 20 48 4 92.31 30 50 2 96.15 40 51 1 98.08 50 51 0 98.08 60 51 0 98.08 70 51 0 98.08 80 52 1 100 90 52 0 100 100+ 52 0 100
Comments
Post a Comment