StuartBombay
Programmer
We have a customer table, orders table, and customer plan table. We need to find customers on a certain plan that have not placed an order in over 3 months. Some customers have never placed an order at all, we need to know who they are too.
Here is my code, the problem is if a customer has placed an order within the last 3 months, they still show in the results but with an order date of 'null'. Ideally if they've placed an order within the last 3 months they wouldn't show at all.
Help please?
Here is my code, the problem is if a customer has placed an order within the last 3 months, they still show in the results but with an order date of 'null'. Ideally if they've placed an order within the last 3 months they wouldn't show at all.
Help please?
Code:
SELECT
a.CustomerID
, LastOrderDate
FROM Customer a
INNER JOIN
(
SELECT
y.PlanID
FROM Plans y
WHERE y.PlanName = 'Internet'
) c
ON a."Plan" = c.PlanID
LEFT JOIN
(
SELECT
x.CustomerID,
MAX(x.OrderDate) AS LastOrderDate
FROM Orders x
GROUP BY x.CustomerID
HAVING MAX(x.OrderDate) <= (DATEADD(month, -4, GETDATE()))
)b
ON
a.CustomerID = b.CustomerID
ORDER BY LastOrderDate