Question : Update Table query from select

I am trying to write an update procedure that will run every first of the month. The table will have 3 of many columns pre populated with Year,Month,RepNum

I have the update query below and when i run it for say month January it will update the corresonpding  rows just fine but will also wipe out all CallsClosed data in other rows. Example.
When i run the query with -1 (for january of that year) it will update the CallsClosed field for
Year      Month    RepNum
2007       1            TA9999

but if i had callsclosed populated already for say  2007    3      TA999  then the value for CallsClosed is nulled by my procedure.  what am i doing wrong?

Thank you

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
Update RepTab
		SET RepTab.CallsClosed =
		
(
	SELECT     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
WHERE     (Year = RepTab.Year) AND (Month = RepTab.Month) AND (RepNum = RepTab.RepNum)
)

from Visiondb.dbo.tbl_MonthlyMetricRep RepTab

Answer : Update Table query from select

You might try joining your FOO table to RepTab.

like this...

(TEST FIRST!!!)

Best Regards

-G

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
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) 
Random Solutions  
 
programming4us programming4us