Question : MSSQL - How to update table with data from another table

Hi,

I have 3 tables. I want to update a table wth result from another table.
TableA-> Fields are: SessionID, RoomID
TableB-> Fields are: SessionID, StaffID
TableC-> Fields are: RoomID, RoomSize

I need to count the number of occurence of each SessionID in TableB and update the data into TableC in the RoomSize field. But I need to get the RoomID from TableA for each of SessionID in TableB (hence TableA serve as a reference table to map sessions to rooms)

Eg: TableA (eg only, actual table has more data)
C101,RM1
C102,RM3
C201,RM4
C103,RM2

TableB (eg only, actual table has more data)
C101,1
C201,1
C101,2
C103,2
C101,4
C201,7
C201,3

TableC
I need to update TableC with data from TableA and TableB
such that C will have:
RoomID, RoomSize
RM1,3 (bec TableB has 3 occurence of C101)
RM2,1 (bec TableB has 1 occurence of C103)
RM3,0 (bec TableB has no occurence of C102)
RM4,3 (bec TableB has 3 occurence of C201)

Will like to be able to do the updating of TableC with 1 query statement.
Possible?

Thanks

Answer : MSSQL - How to update table with data from another table

Ok, in that case this should do:
1:
2:
3:
4:
5:
6:
UPDATE TableC
SET RoomSize = t2.cnt
FROM TableC t1 inner join 
(SELECT t1.RoomID , COUNT(*) cnt
FROM TableA t1 inner join TableB t2 on t1.SessionID = t2.SessionID
GROUP BY t1.RoomID) t2 on t1.RoomID = t2.RoomID
Random Solutions  
 
programming4us programming4us