我有一个三个 mysql 数据库表,其中包含客户公司的通话记录。我想每小时从数据库中获取公司组的数据。如果一个小时没有数据,它应该返回零。
我正在执行此查询,但仅在数据库中有数据时才获得该小时的结果。
这是我的查询。
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);
这是得到的结果。
+---------+-----------+-----------------+--------+----------------+
| 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 |
+---------+-----------+-----------------+--------+----------------+
但我想要从零小时到 23 小时的结果。如果没有数据,那么它应该返回零。