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 2000 Forums
 SQL Server Development (2000)
 Update Records using results from a Select

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-05-24 : 09:45:23
MichaelAJ writes "I have three tables set up for a contest. Users, Games, and Picks. The Users table stores the point totals, and the UserID. The Picks table stores the GameID,WinnerID, and UserID. The Games table stores the GameID,Winner and the Game value. After scores are enterd and a winner for a game is determined, points must be given to the users who guessed the correct winners. So I wrote the following SQL to return the UserID and their total points:

"Select Picks.UserID,Sum(Games.Points) as TotalPoints From Picks Inner Join Games on (Picks.WinnerID=Games.Winner and Picks.GameID=Games.GameID) Group By Picks.UserID"

From the Results, I need to update the Users table and insert their points. How would I merge this SQL into an Update to make it happen in one pass? I'm running on SQL Server 2000

Thanks for your help"

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-05-24 : 10:43:29
Post your CREATE TABLE scripts and I'll walk you through it

quote:

MichaelAJ writes "I have three tables set up for a contest. Users, Games, and Picks. The Users table stores the point totals, and the UserID. The Picks table stores the GameID,WinnerID, and UserID. The Games table stores the GameID,Winner and the Game value. After scores are enterd and a winner for a game is determined, points must be given to the users who guessed the correct winners. So I wrote the following SQL to return the UserID and their total points:

"Select Picks.UserID,Sum(Games.Points) as TotalPoints From Picks Inner Join Games on (Picks.WinnerID=Games.Winner and Picks.GameID=Games.GameID) Group By Picks.UserID"

From the Results, I need to update the Users table and insert their points. How would I merge this SQL into an Update to make it happen in one pass? I'm running on SQL Server 2000

Thanks for your help"



setBasedIsTheTruepath
<O>
Go to Top of Page

aclarke
Posting Yak Master

133 Posts

Posted - 2002-05-25 : 04:12:51
Something like this should work. Maybe not the fanciest way, but hey.


select picks.userID, Sum(Games.Points) as totalPoints
into #foo
From Picks Inner Join Games on (Picks.WinnerID=Games.Winner and Picks.GameID=Games.GameID)
Group By Picks.UserID

update users
set users.points = #foo.totalPoints
from users, #foo
where users.userID = #foo.userID

drop table #foo


Go to Top of Page
   

- Advertisement -