I have a three mysql database table which contains the call record of the customer company. I want to fetch the data from the database for a company group by per hour. If there is no data for an hour, it should return Zero.
I am executing this query, but am getting the result for that hour only for which there is a data in the database.
This is my query.
select ph_Plans.Comp_ID, Plan_Type, ph_Companies.CompanyName,
(sum(call_length_billable)*100)/(Plan_Limit*60) as total,
hour(calldate)
from ph_Plans
join ph_Companies
on ph_Companies.Comp_ID = ph_Plans.Comp_ID
join cdr
on cdr.CompanyName = ph_Companies.CompanyName
where Plan_Type='Per_Min'
and date(calldate)='2012-10-01'
and ph_Companies.CompanyName='"ReadySpace-EN"'
group by hour(calldate);
This is the result that am getting.
+---------+-----------+-----------------+--------+----------------+
| Comp_ID | Plan_Type | CompanyName | total | hour(calldate) |
+---------+-----------+-----------------+--------+----------------+
| 44 | Per_Min | "ReadySpace-EN" | 3.7467 | 1 |
| 44 | Per_Min | "ReadySpace-EN" | 9.4933 | 18 |
| 44 | Per_Min | "ReadySpace-EN" | 1.6600 | 20 |
| 44 | Per_Min | "ReadySpace-EN" | 3.7333 | 21 |
| 44 | Per_Min | "ReadySpace-EN" | 4.6067 | 2 |
| 44 | Per_Min | "ReadySpace-EN" | 7.6533 | 23 |
+---------+-----------+-----------------+--------+----------------+
But I want the result from zeroth hour to 23 hour. If no data then it should return zero.
When doing a
JOIN, you onlyJOINon tables that both have data that corresponds to the Columns being JOINed. What you want to do is aLEFT JOINwhen you still want results where there is no corresponding data in the other table.Something like this: