Try something like this:SELECT C.customer_name, C.subscription_date, COUNT(*) AS orders_count FROM Customers AS C INNER JOIN (SELECT O.customer_id, DATEADD(MONTH, DATEDIFF(MONTH, 0, O.order_date), 0) AS date FROM Orders AS O GROUP BY O.customer_id, DATEADD(MONTH, DATEDIFF(MONTH, 0, O.order_date), 0)) AS O ON O.customer_id = C.customer_id AND O.date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, C.subscription_date), 0) AND O.date <= GETDATE() WHERE C.subscription_date <= GETDATE() GROUP BY C.customer_name, C.subscription_dateHAVING COUNT(*) = DATEDIFF(MONTH, C.subscription_date, GETDATE()) + 1;
For us, there is only the trying. The rest is not our business. ~T.S. EliotMuhammad Al Pasha