Below is my exact code as it sits. This query works excellent except for when the teamid does not exist inside the date range. If the teamid does not exist within the date range specified, I want SQL to add the teamid to the table and set its count to 0. Then, my table will automatically convert the teamid to the teamtext for me.
SELECT tn.teamtext, COUNT(referteamID2) AS cnt
FROM teamnames AS tn
JOIN caseaudit AS ca
ON tn.teamID = ca.referteamID2
WHERE CONVERT(smalldatetime,dModLast,101) BETWEEN '2012-03-01' AND '2012-03-10'
AND ca.referteamid1 <> ca.referteamid2
AND teamid IN (99, 107, 124, 27, 31, 44, 110, 43, 57, 50, 46)
GROUP BY tn.teamtext
ORDER BY tn.teamtext
This returns:
4H BOSS 59
ASSET MANAGEMENT 16
BOSS 5
CUSTOMER SUPPORT 133
NETWORK ENGINEERING 15
PRODUCTION ENGINEERING 142
SECURITY 6
VOICE SERVICES 21
XEROX 8
I was told that the problem may possibly have something to do with using IN after giving the date constraints.
Move the condition from the WHERE clause to a CASE statement in the SELECT: