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)
 Update subquery

Author  Topic 

Deltaur
Starting Member

29 Posts

Posted - 2012-02-15 : 03:23:39
Hi,

Simple problem. I got two tables 1-N ratio.
TABLE 1(1) contains orders main information (Order number which is PK, and other information). TABLE 2(N) contains order rows and there may be N amount rows for every order in TABLE 1.

So, i have to create update command which updates value TOTALPIECES in my TABLE 3 (which contains all information for order in one row) SUM(pieces in TABLE 2) from every order in TABLE 1. So after that i could get all the information in one result.

TABLE1:
ORDERNUM,CUSTOMER,DATE

TABLE2:
ROWID,ORDERNUM,PRODUCT,PIECES

TABLE3:
ORDERNUM,CUSTOMER,TOTALPIECES


Deltaur
Starting Member

29 Posts

Posted - 2012-02-15 : 04:20:06
I got it now.

i have the order numbers inserted to TABLE 3 so i dont need the TABLE 1 values.

UPDATE TB3 SET
TOTALPIECES = (select isnull(sum(pieces),0)
from TABLE2 TB2
where TB2.ORDERNUM= TB3.ORDERNUM))
FROM TABLE3 TB3

Go to Top of Page
   

- Advertisement -