Question : Joining 3 tables

select c.id,g.desc, date(c.call_start) as date_call, hour(c.call_start) as hour_call, concat(floor(avg(1.0*c.duration)/60),':',round(avg(1.0*c.duration)%60)) as avg_duration_mm_ss_string
from calls c inner join `t`.`gate` g on c.id=g.id  where c.call_start > '2010-07-04 00:00:00' and c.call_start < '2010-07-04 23:59:59' group by  c.id,g.desc,date(c.call_start), hour(c.call_start) order by 1,2,3 ;

Now I also have a table names callsf as cf .That also has call_start .Now i need to check the count of call_start at callf at every hour based on ID and then i have to do count(c.call_start)/(count(c.call_start)+count(cf.call_start))

I need all that in one query.

Answer : Joining 3 tables

What do you get for

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)

?
Does it line up (by id, date, hour) against the below?

      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)

The code box shouldn't make a difference (swapped inner/left join table order), but give it a try anyway.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
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
Random Solutions  
 
programming4us programming4us