以下查询有效,但我想稍微修改一下。
实际上,它从客户表中选择了来自此文件中前一个函数的数组中的所有客户(因此' NOT IN($ dealerNum')。查询所有其他客户,并检查他们的号码,如果他们在过去6个月之前的12个月内有订单(我在18个月的窗口工作)。
我需要稍微改变一下。我仍然想在任何不在前一个阵列中的客户上运行它。但是,我想检索过去6个月内有订单的客户的数量,而不是之前的12个月。基本上显示他们至少在那一年/ 12个月内活跃并且在过去的6个月中一直活跃。
希望这是有道理的。这是现有的查询,我只是不确定修改我的WHERE以处理此问题的最佳/最合理的方法
SELECT c.customerNumber
FROM Customers C
INNER JOIN Orders G ON C.CustomerNumber = G.Customer
WHERE c.customerNumber NOT IN ($dealerNum)
AND C.date BETWEEN CURRENT_DATE - 18 MONTHS AND CURRENT_DATE - 6 MONTHS
GROUP BY C.customerNumber
HAVING COUNT(*) > 0
答案 0 :(得分:2)
我会像这样写
SELECT c.customerNumber
FROM Customers C
WHERE c.customerNumber NOT IN ($dealerNum)
AND c.customerNumber IN (SELECT Customer FROM Orders where Date BETWEEN CURRENT_DATE - 6 MONTHS AND CURRENT_DATE )
AND c.customerNumber NOT IN (SELECT Customer FROM Orders where Date BETWEEN CURRENT_DATE - 18 MONTHS AND CURRENT_DATE - 6 MONTHS)
答案 1 :(得分:1)
select c.customerNumber
from Customers c
join Orders o
on c.CustomerNumber = o.Customer
where c.customerNumber not in ($dealerNum)
group by
c.customerNumber
having count(case when o.date > current date - 6 months then 1 end) > 0
and count(case when o.date between current date - 18 months and
current_date - 6 months then 1 end) = 0