Update RepTab
SET RepTab.CallsClosed = CallsClosed
FROM (SELECT DISTINCT
DATEPART(YYYY, SERV_CALL.COMPLETE_DATE_SCA) AS Year
, DATEPART(MM, SERV_CALL.COMPLETE_DATE_SCA) AS Month
, SERV_CALL.REPRESENTATIVE AS RepNum
, COUNT(*) AS CallsClosed
FROM SERV_CALL
INNER JOIN PR_PERSONNEL ON SERV_CALL.REPRESENTATIVE = PR_PERSONNEL.REPRESENTATIVE
INNER JOIN PR_EMPL_NAME ON PR_PERSONNEL.EMPLOYEE_NO = PR_EMPL_NAME.EMPLOYEE_NO
WHERE (DATEPART(YYYY, SERV_CALL.COMPLETE_DATE_SCA) = DATEPART(YYYY, GETDATE())) AND (DATEPART(MM, SERV_CALL.COMPLETE_DATE_SCA)
= DATEPART(MM, DATEADD(MM, - 1, GETDATE())))
GROUP BY DATEPART(YYYY, SERV_CALL.COMPLETE_DATE_SCA)
, DATEPART(MM, SERV_CALL.COMPLETE_DATE_SCA)
, SERV_CALL.REPRESENTATIVE) AS foo (year, Month, RepNum, CallsClosed)
join Visiondb.dbo.tbl_MonthlyMetricRep RepTab on (RepTab.RepNum = foo.RepNum)
and (foo.Year = RepTab.Year) AND (foo.Month = RepTab.Month)
|