Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SP Help

Author  Topic 

ann
Posting Yak Master

220 Posts

Posted - 2009-11-20 : 16:54:02
I need to update a table with a new field based on the information (count) on another table:

Table1:
ID Title *PgCount(new Field - int)
1 abc
2 def
3 ghi


table2:
tbl2ID ID
1 1
2 1
3 1
4 2
5 2
3 3

Results in Table1:
ID Title *PgCount(new Field - int)
1 abc 3
2 def 2
3 ghi 1


I know how to do a count and I know how to do an update, what I don't know is how to combine the 2 to get the above results.

Can anyone help on this?

Thanks

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-11-20 : 17:19:10
UPDATE A SET PGCount=Z.Cnt
FROM Table1 A
INNER JOIN (SELECT Tbl2ID, count(*) Cnt FROM Table2 GROUP BY Tbl2ID) Z
ON A.ID=Z.Tbl2ID
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-20 : 17:42:43
Ann,

You do understand, that the data that you collect for the count, is a stale as the moment after the query runs

What value is this?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -