I have a temp table I am creating a query off of in the following format. That contains a record for every CustomerID, Year, and Month for several years.
#T
Customer | CustomerID | Year | Month
ex.
Foo | 12345 | 2008 | 12 Foo | 12345 | 2008 | 11 Bar | 11224 | 2007 | 7
When I join this temp table to another table of the following format I get many more results than I am expecting.
Event
EventID | CustomerID | DateOpened
ex.
1100 | 12345 | '2008-12-11 10:15:43' 1100 | 12345 | '2008-12-11 11:25:17'
I am trying to get a result set of the count of events along with the Customer, Year, and Month like this.
SELECT COUNT(EventID), Customer, Year, Month FROM [Event] JOIN #T ON [Event].CustomerID = #T.CustomerID WHERE [Event].DateOpened BETWEEN '2008-12-01' AND '2008-12-31' GROUP BY Customer, Year, Month ORDER BY Year, Month
I am getting a record for every Year and Month instead of only for December 2008.
You’re specifying the date on the event table but not on the join — so it’s joining all records from the temp table with a matching customerid.
Try this: