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 |
|
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 2000Thanks 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 itquote: 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 2000Thanks for your help"
setBasedIsTheTruepath<O> |
 |
|
|
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 totalPointsinto #fooFrom Picks Inner Join Games on (Picks.WinnerID=Games.Winner and Picks.GameID=Games.GameID)Group By Picks.UserIDupdate usersset users.points = #foo.totalPointsfrom users, #foowhere users.userID = #foo.userIDdrop table #foo |
 |
|
|
|
|
|
|
|