I have a table with customer names, items and dates added
I need a select statement that returns the customer name but only where all items purchased were last month. Example, all purchases for Cust_1 were last month so this customer is returned but Cust2 and Cust_3 had purchases in other months so they are not returned.
I have the date range sorted out and have tried various ‘Group By’ and ‘Having’ clauses but im struggling due to it being dates and not strings.
HAVING clause with
MAX on your
DAteAdded Column. You can easily create a date boundary with
SELECT Cust_ID FROM dbo.YourTable GROUP BY Cust_ID HAVING MIN(DateAdded) >= DATEADD(DAY, 1, EOMONTH(GETDATE(),-2)) AND MAX(DateAdded) < DATEADD(DAY, 1, EOMONTH(GETDATE(),-1));