4

我被要求报告我们客户的联系频率,即每周、每月、每季度或每年看到我们的客户数量。

当在论坛中讨论“频率”时,它们通常是指某个值在表格中存在的次数。

我可以获得客户的联系人数量:-

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)

然而,我正在努力融入分发的复杂性。例如,有些客户有很多联系人,但他们可能一开始每周都有联系,现在他们只有每年一次的检查。

我可能会接受这是不可能的,但如果有人做过类似的工作,我会很感激你的见解。

非常感谢

4

1 回答 1

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 
于 2013-04-11T08:22:23.733 回答