This is sorta a 2 part question although I am not sure if I should put them as one question...oh well
FIRST
I am after a bit of help constructing a SQL query to return the correct results which are to populate a dropdown list from a StorProc. Its part of site for adding scores for squash matches and the dropdown is supposed to list all players in your division that you have NOT played a game against yet. When a game is played someone would log into the site, select the person from a dropdown list and enter scores for both sides (either player can do this). The script creates a game, takes the new gameID, adds the points from that match to the points total in the table 'nykSqPlayers' and populates the rest of the relevant tables below and then refreshes the dropdown. However, after initially entering the score for Test User1 against Test User2 the dropdown isn't listing anyone. So I tested the SQL below in Server Management and it doesn't return any data. It should return Test User 3 and Test User 4 (in theory).
Format will be:
Tables (Fields)
Data
nykGame (ID int IDENTITY, Date dateimte, UserID int)
25, 01/04/2009 13:41:53, 3
nykScore (ID int IDENTITY, nykGameID int, UserID int, Score int, Points int)
21, 25, 3, 3, 5
22, 25, 4, 1, 2
nykSqPlayers (UserID int, DivisionID int, UserName nvarchar(250), Points int)
3, 1, Test User1, 5
4, 1, Test User2, 2
5, 1, Test User3, 0
6, 1, Test User4, 0
What I have managed so far is (replace @UserID with the relevant UserID from nykSqPlayers).:
SELECT nykSqPlayers.*, nykScore.nykGameID
FROM nykScore RIGHT OUTER JOIN nykSqPlayers ON nykScore.UserID = nykSqPlayers.UserId
WHERE DivisionId= (select DivisionId from dbo.nykSqPlayers where UserId= @UserID) AND nykSqPlayers.UserId <> @UserID AND nykGameID NOT IN (SELECT nykGameID FROM nykScore WHERE UserID=@UserID) AND nykSqPlayers.UserId NOT IN (SELECT nykScore_1.UserID AS OUserID FROM Users AS Users_1 INNER JOIN nykScore AS nykScore_1 ON Users_1.UserID = nykScore_1.UserID INNER JOIN nykScore INNER JOIN Users ON nykScore.UserID = Users.UserID INNER JOIN nykGame ON nykScore.nykGameID = nykGame.ID ON nykScore_1.nykGameID = nykGame.ID WHERE (nykScore.UserID = @UserID) AND (nykScore_1.UserID <> @UserID))
My 2nd request is:
Is it possible to constract a score table setup purely through SQL so it would return something like:
| Test User1 | Test User2 | Test User3 | Test User4 | Total Points
------------------------------------------------------------------------------------------------
Test User 1 | N/A | 3-1 | Null | Null | 5
------------------------------------------------------------------------------------------------
Test User 2 | 1-3 | N/A | Null | Null | 2
------------------------------------------------------------------------------------------------
Test User 3 | Null | Null | Null | Null | 0
------------------------------------------------------------------------------------------------
Test User4 | Null | Null | Null | Null | 0
------------------------------------------------------------------------------------------------
Any help with this would be greatly appreciated...been working on this through the night
Almost forgot a brief explanation of the relationship of the tables.
nykGame.ID -> nykScore.nykGameID - nykScore.UserID -> nykSqPlayers.UserID
The UserID in nykGame plays no role in this situation...its only there to act as identifying who entered the scores for both players.
- FateFirst
FIRST
I am after a bit of help constructing a SQL query to return the correct results which are to populate a dropdown list from a StorProc. Its part of site for adding scores for squash matches and the dropdown is supposed to list all players in your division that you have NOT played a game against yet. When a game is played someone would log into the site, select the person from a dropdown list and enter scores for both sides (either player can do this). The script creates a game, takes the new gameID, adds the points from that match to the points total in the table 'nykSqPlayers' and populates the rest of the relevant tables below and then refreshes the dropdown. However, after initially entering the score for Test User1 against Test User2 the dropdown isn't listing anyone. So I tested the SQL below in Server Management and it doesn't return any data. It should return Test User 3 and Test User 4 (in theory).
Format will be:
Tables (Fields)
Data
nykGame (ID int IDENTITY, Date dateimte, UserID int)
25, 01/04/2009 13:41:53, 3
nykScore (ID int IDENTITY, nykGameID int, UserID int, Score int, Points int)
21, 25, 3, 3, 5
22, 25, 4, 1, 2
nykSqPlayers (UserID int, DivisionID int, UserName nvarchar(250), Points int)
3, 1, Test User1, 5
4, 1, Test User2, 2
5, 1, Test User3, 0
6, 1, Test User4, 0
What I have managed so far is (replace @UserID with the relevant UserID from nykSqPlayers).:
SELECT nykSqPlayers.*, nykScore.nykGameID
FROM nykScore RIGHT OUTER JOIN nykSqPlayers ON nykScore.UserID = nykSqPlayers.UserId
WHERE DivisionId= (select DivisionId from dbo.nykSqPlayers where UserId= @UserID) AND nykSqPlayers.UserId <> @UserID AND nykGameID NOT IN (SELECT nykGameID FROM nykScore WHERE UserID=@UserID) AND nykSqPlayers.UserId NOT IN (SELECT nykScore_1.UserID AS OUserID FROM Users AS Users_1 INNER JOIN nykScore AS nykScore_1 ON Users_1.UserID = nykScore_1.UserID INNER JOIN nykScore INNER JOIN Users ON nykScore.UserID = Users.UserID INNER JOIN nykGame ON nykScore.nykGameID = nykGame.ID ON nykScore_1.nykGameID = nykGame.ID WHERE (nykScore.UserID = @UserID) AND (nykScore_1.UserID <> @UserID))
My 2nd request is:
Is it possible to constract a score table setup purely through SQL so it would return something like:
| Test User1 | Test User2 | Test User3 | Test User4 | Total Points
------------------------------------------------------------------------------------------------
Test User 1 | N/A | 3-1 | Null | Null | 5
------------------------------------------------------------------------------------------------
Test User 2 | 1-3 | N/A | Null | Null | 2
------------------------------------------------------------------------------------------------
Test User 3 | Null | Null | Null | Null | 0
------------------------------------------------------------------------------------------------
Test User4 | Null | Null | Null | Null | 0
------------------------------------------------------------------------------------------------
Any help with this would be greatly appreciated...been working on this through the night
Almost forgot a brief explanation of the relationship of the tables.
nykGame.ID -> nykScore.nykGameID - nykScore.UserID -> nykSqPlayers.UserID
The UserID in nykGame plays no role in this situation...its only there to act as identifying who entered the scores for both players.
- FateFirst