我被要求报告我们客户的联系频率,即每周,每月,每季度或每年看多少客户。
在论坛中讨论“频率”时,它们通常指的是表中存在某个值的次数。
我可以获得客户的联系人数量: -
select
A.client_ID, A.start_date, A.Months_in_Service,
B.Contacts, (B.Contacts / A.Months_in_Service) as Contacts_per_Month_in_Service
from
tbl_client A
left outer join
(select
client_ID, COUNT(contact_date) as Contacts
from
tbl_client_contact
group by
client_ID) as B ON A.client_ID = B.client_ID
where
(A.end_date is null)
然而,我正在努力将分配的复杂性纳入其中。例如,一些客户有很多联系人,但他们可能已经开始每周联系,现在他们只进行年度检查。
我可能会接受这是不可能的,但如果那里有人做了类似的工作,我会很感激你的见解。
非常感谢
答案 0 :(得分:0)
试试这个。我不确定因为我无法查看任何样本数据输出。但试试吧。
SELECT A.client_ID, A.start_date, A.Months_in_Service, B.Contacts2,
AVG(B.Contacts2 / A.Months_in_Service) as Contacts_per_Month_in_Service
FROM
(
select Contacts, Count(Months_in_Service) as Contacts
from tbl_client
) AS A
LEFT JOIN
(
select client_ID, COUNT(contact_date) as Contacts2
from tbl_client_contact
group by client_ID
) as B ON A.client_ID = B.client_ID
WHERE (A.end_date is null)
GROUP BY A.client_ID, A.start_date, A.Months_in_Service,
B.Contacts, Contacts_per_Month_in_Service