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.
| Author |
Topic |
|
nickless
Starting Member
1 Post |
Posted - 2005-07-26 : 21:46:46
|
| Hie all, can someone provide me with a sql command that do the following:a) get USERID from Table A,b) get the total of adding up column Total_Price from Table B where USERID of Table B = USERID from Table A,c) Update the total obtain from (b) into Table C where USERID of Table C = USERID from Table A.d) repeat (a) until no USERID is found.This code is intended to be used as a Job. Please help........TQ. |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2005-07-26 : 23:18:12
|
Should be able to do it in a single statement:Update [Table C]SET columnName = (SELECT SUM(Total_Price) FROM [Table B] WHERE UserID = C.UserID)FROM [Table C] C Note that you don't need to reference Table A at all, because you're only interested in Users that have related records in Table B.HTH,Tim |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-07-27 : 00:21:51
|
quote: Originally posted by timmy
Update [Table C]SET columnName = (SELECT SUM(Total_Price) FROM [Table B] WHERE UserID = C.UserID)FROM [Table C] C Note that you don't need to reference Table A at all, because you're only interested in Users that have related records in Table B.
I don't know the data, but maybe TableC has some UserIDs that are not in TableA? |
 |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2005-07-27 : 01:30:40
|
quote: Originally posted by SamC
quote: Originally posted by timmy
Update [Table C]SET columnName = (SELECT SUM(Total_Price) FROM [Table B] WHERE UserID = C.UserID)FROM [Table C] C Note that you don't need to reference Table A at all, because you're only interested in Users that have related records in Table B.
I don't know the data, but maybe TableC has some UserIDs that are not in TableA?
It was a bold assumption on my part, I do admit..... |
 |
|
|
|
|
|