表:reception_log,add_time为接待时间,customer_id为客户id
表:project_customer,flag为客户类型,id为客户id
查询 时间段(2024 年 8 月到 12 月)接待的复访客户
2024-08-01 - 2028-12-31
复访定义:
复访客户是指曾经多次到访的客户。可以通过检查客户的历史到访记录来定义复访。
根据上述筛选来说,客户至少需要在2024年8月到12月之前的某个时间点访问过,并且在2024年8月到12月之间再次访问。
注意
reception 中 同一个 customer_id 可出现多次
需要获取的是复访客户数,而不是重复到访次数
方法1、RIGHT JOIN
SELECT count(log1.uid) as mcount FROM reception AS log1 right JOIN ( SELECT uid FROM reception WHERE add_time<='2024-12-31' GROUP BY customer_id HAVING COUNT(*) > 1 ) AS log2 ON log1.customer_id = log2.customer_id WHERE log1.add_time>='2024-08-01' and log1.add_time<='2024-12-31'
子查询本身已经筛选出符合条件的 uid,可以用 INNER JOIN 来实现
方法2、INNER JOIN
SELECT COUNT(DISTINCT log1.uid) AS mcount
FROM reception AS log1
INNER JOIN (
SELECT uid
FROM reception
WHERE add_time <= ‘2024-12-31’
GROUP BY uid
HAVING COUNT(*) > 1
) AS log2 ON log1.uid = log2.uid
WHERE log1.add_time >= ‘2024-08-01’
AND log1.add_time <= ‘2024-12-31’