Question : Update table basxed on results of query

Experts, I have the following temp table @tmpContacts:

ContactID   ClientID   NumContactsNeeded   NumContactsMade   TimePeriod
-------------   ----------  ---------------------------   ------------------------   --------------
     1                3                        2                              NULL                   Day
     2                4                        1                              NULL                 Week
     3                5                        1                              NULL                 Month

I am trying to update the NumContactsMade field using the following query:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
UPDATE @tmpContacts
SET NumContactsMade = b.Contacts FROM
	(
		SELECT ClientID, SUM(Contacts) AS Contacts 
		FROM 
			(
				SELECT ClientID, COUNT(Client_Contact_Tbl.ClientID) AS Contacts
				FROM Client_Contact_Tbl
				GROUP BY ClientID, Contact_Status, ContactDate
				HAVING Contact_Status IS NOT NULL AND ContactDate >= '6/20/2010'
			) a
		GROUP BY a.ClientID
	) b, @tmpContacts


Which is giving me the following results:

ContactID   ClientID   NumContactsNeeded   NumContactsMade   TimePeriod
-------------   ----------  ---------------------------   ------------------------   --------------
     1                3                        2                                1                          Day
     2                4                        1                                1                        Week
     3                5                        1                                1                        Month

However, the results should be:

ContactID   ClientID   NumContactsNeeded   NumContactsMade   TimePeriod
-------------   ----------  ---------------------------   ------------------------   --------------
     1                3                        2                                 1                          Day
     2                4                        1                                 2                         Week
     3                5                        1                              NULL                 Month

Since the results of the sub query are:

ClientID   Contacts
----------   -----------
      3              1
      4              2
     13             1

What am I doing wrong?

Answer : Update table basxed on results of query

update tempAlias
set NumContactsMade=Contacts
from
(
            SELECT ClientID, SUM(Contacts) AS Contacts
            FROM
                  (
                        SELECT ClientID, COUNT(Client_Contact_Tbl.ClientID) AS Contacts
                        FROM Client_Contact_Tbl
                        GROUP BY ClientID, Contact_Status, ContactDate
                        HAVING Contact_Status IS NOT NULL AND ContactDate >= '6/20/2010'
                  ) a
            GROUP BY a.ClientID
) derivedTable
join @tempContacts tempAlias on derivedTable.ClientId=tempAlias.ClientId

Syntactically that should be correct (although admittedly I think it could be cleaned up to eliminate the derived table).  Logically I wonder if your subquery should be grouping on contactid rather than client id, but I suppose if contact and client are 1:1 it won't matter.
Random Solutions  
 
programming4us programming4us