表格调用c
CALL_ID | CALL_START | CONTACT_ID | CALL_TYPE
--------------------------------------------------
2 | 25/11/2010 11:28:16 | 850 | I
3 | 25/11/2010 11:28:57 | 850 | I
5 | 29/11/2010 10:18:44 | 848 | I
表联系人ct
CONTACT_ID | COMPANY_ID | RECORD_STATUS
-----------------------------------
1 | 1 | A
19 | 2 | A
20 | 3 | A
21 | 4 | A
22 | 5 | A
我想提取每月的来电和去电次数 对于一个特定的Company_id和所有其他公司。
我还需要在0700到1500/1500和2200/2200 0700之间的平均通话次数
所有从2016年10月开始,我有一个运营商ID的列表,包括......
select
extract(year from c.call_start) as MyYear,
extract(month from c.call_start) as MyMonth,
count(case when call_type='I' and ct.Company_id not like 391 then 1 else null end) as Incoming_Main,
count(case when call_type='O' and ct.Company_id not like 391 then 1 else null end) as Outgoing_Main,
count(case when call_type='I' and ct.Company_id = 391 then 1 else null end) as Incoming_SMG,
count(case when call_type='O' and ct.Company_id = 391 then 1 else null end) as Outgoing_SMG,
SUM(CASE WHEN CAST(c.CALL_START AS TIME) BETWEEN '07:00:00' and '15:00:00' and ct.company_id=391 THEN 1 ELSE 0 END)/31 Am391,
SUM(CASE WHEN CAST(c.CALL_START AS TIME) BETWEEN '07:00:00' and '15:00:00' and ct.company_id not like 391 ELSE 0 END)/31 AmMAIN,
SUM(CASE WHEN CAST(c.CALL_START AS TIME) BETWEEN '15:00:00' and '22:00:00' and ct.company_id =391 THEN 1 ELSE 0 END)/31 PM391,
SUM(CASE WHEN CAST(c.CALL_START AS TIME) BETWEEN '15:00:00' and '22:00:00' and ct.company_id=391 THEN 1 ELSE 0 END)/31 PmMAIN,
SUM(CASE WHEN CAST(c.CALL_START AS TIME) BETWEEN '07:00:00' and '15:00:00' and ct.company_id=391 THEN 1 ELSE 0 END)/31 Am391,
SUM(CASE WHEN CAST(c.CALL_START AS TIME) BETWEEN '07:00:00' and '15:00:00' and ct.company_id=391 THEN 1 ELSE 0 END)/31 Am391,
from
CALLS c
left outer join CONTACTS ct on c.CONTACT_ID= ct.CONTACT_ID
where ct.RECORD_STATUS='A'
and c.OPERATOR_ID in (1,19,22)
and c.call_start >='2016/10/01