select c.id, g.desc, c.d as date_call, c.h as hour_call,
count_c / NULLIF(count_c + IFNULL(count_cf,0),0) as SomePercentage
from `t`.`gate` g
inner join (
select c.id, date(c.call_start) as d, hour(c.call_start) as h, count(c.call_start) as count_c,
concat(floor(avg(1.0*c.duration)/60),':',round(avg(1.0*c.duration)%60)) as avg_duration_mm_ss_string
from calls c
where c.call_start >= '2010-07-04 00:00:00' and c.call_start < '2010-07-04 23:59:59'
group by c.id, date(c.call_start), hour(c.call_start)
) c on c.id=g.id
left join (
select cf.id, date(cf.call_start) as d, hour(cf.call_start) as h, count(cf.call_start) as count_cf
from callsf cf
where cf.call_start >= '2010-07-04 00:00:00' and cf.call_start < '2010-07-04 23:59:59'
group by cf.id, date(cf.call_start), hour(cf.call_start)
) cf on cf.id=c.id and c.d=cf.d and c.h=cf.h
order by c.id, g.desc, c.d, c.h
|