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 |
|
fizban2
Starting Member
4 Posts |
Posted - 2008-01-30 : 16:56:44
|
Hi all, trying to learn a little SQL and find myself stumped, i have 3 tables, one for Events, for players, and availability. i am trying to return a to query each event and retrieve all the players who are available. SELECT DISTINCT dbo.Tournaments.EventName, dbo.Tournaments.EventDate, dbo.Tournaments.EventScheduled, dbo.Tournaments.EventPlayed, dbo.Tournaments.Location, dbo.Availability.Status, dbo.Players.FirstName, dbo.Players.LastNameFROM dbo.Availability INNER JOIN dbo.Players ON dbo.Availability.PlayerID = dbo.Players.PlayerID INNER JOIN dbo.Tournaments ON dbo.Availability.EventID = dbo.Tournaments.EventID i get all the events, but multiple events, i just need each event listed one, but all players for that event. Thanks! |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-30 : 17:06:15
|
| Can you provide sample data for each of your 3 tables and an example of what you would like to have returned? |
 |
|
|
fizban2
Starting Member
4 Posts |
Posted - 2008-01-30 : 17:28:05
|
Sure thing-Availability-PlayerID(FK)EventID(FK)Status(bit)-Players-PlayerID(PK)FirstNameLastNameUserName-Tournaments-EventID(pk)EventNameEventDateEventScheduledEventPlayedLocation Availability___________________________EventID|PlayerID|Status|1 | 1 | True |1 | 2 | False|1 | 3 | True |Players________________________________________PLayerID|FirstName|LastName|UserName|1 |Kevin | Smithe | Ksmith |2 |John | Doe | jDoe |3 |Frank | White | fwhite |Event______________________________________________________________________EventID|EventName|Event Date|Event Scheduled|EventPlayed|Location |1 |Tourney1 |2/2/2008 |True | False |Someplace|2 |Tourney2 |2/4/2008 |False | False |Noplace |So what i want to get is Tourney 1 2/2/2008 Someplace Kevin Smith, Frank WhiteThis gets me close but i don't know how to get rid of the multiple event names and dates etc. SELECT t.EventName, t.EventDate, t.Location, p.FirstName + ' ' + p.LastName as 'Players' From Availability as a INNER JOIN Players as p ON a.PlayerID = p.PlayerID INNER JOIN Tournaments as t ON a.EventID = t.EventIDWHERE t.EventScheduled = 'True' AND a.Status = 'TRUE' |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-30 : 17:49:51
|
You could run it as follows:/* Example of Availability Table */DECLARE @Availability TABLE ( EventID INT, PlayerID INT, Status CHAR(5) )INSERT @Availability ( EventID, PlayerID, Status )SELECT 1, 1, 'True' UNIONSELECT 1, 2, 'False' UNIONSELECT 1, 3, 'True'/* Example of Players Table */DECLARE @Players TABLE ( PLayerID INT, FirstName VARCHAR(25), LastName VARCHAR(25), UserName VARCHAR(25) )INSERT @Players ( PLayerID, FirstName, LastName, UserName )SELECT 1, 'Kevin', 'Smithe', 'Ksmith' UNIONSELECT 2, 'John', 'Doe', 'jDoe' UNIONSELECT 3, 'Frank', 'White', 'fwhite'/* Example of Event Table */DECLARE @Event TABLE ( EventID INT, EventName VARCHAR(25), EventDate DATETIME, EventScheduled CHAR(5), EventPlayed CHAR(5), Location VARCHAR(25) )INSERT @Event ( EventID, EventName, EventDate, EventScheduled, EventPlayed, Location )SELECT 1, 'Tourney1', '2/2/2008', 'True', 'False', 'Someplace' UNIONSELECT 2, 'Tourney2', '2/4/2008', 'False', 'False', 'Noplace'/* Query */DECLARE @PlayerList VARCHAR(100)SELECT @PlayerList = COALESCE(@PlayerList + ', ' + p.FirstName + ' ' + p.LastName, p.FirstName + ' ' + p.LastName, @PlayerList)FROM @Availability AS a INNER JOIN @Players AS p ON a.PlayerID = p.PlayerID INNER JOIN @Event AS t ON a.EventID = t.EventIDWHERE t.EventScheduled = 'True' AND a.Status = 'True'SELECT t.EventName, t.EventDate, t.Location, @PlayerList AS PlayerListFROM @Availability AS a INNER JOIN @Players AS p ON a.PlayerID = p.PlayerID INNER JOIN @Event AS t ON a.EventID = t.EventIDWHERE t.EventScheduled = 'True' AND a.Status = 'True'GROUP BY t.EventName, t.EventDate, t.Location |
 |
|
|
fizban2
Starting Member
4 Posts |
Posted - 2008-01-30 : 18:05:51
|
i changed it around a little bit and got it to workDECLARE @PlayerList VARCHAR(100) SELECT @PlayerList = COALESCE(@PlayerList + ', ' + p.FirstName + ' ' + p.LastName, p.FirstName + ' ' + p.LastName, @PlayerList)FROM Availability AS a INNER JOIN Players AS p ON a.PlayerID = p.PlayerID INNER JOIN Tournaments AS t ON a.EventID = t.EventIDWHERE t.EventScheduled = 'True' AND a.Status = 'True'SELECT t.EventName, t.EventDate, t.Location, @PlayerList AS PlayerListFROM Availability AS a INNER JOIN Players AS p ON a.PlayerID = p.PlayerID INNER JOIN Tournaments AS t ON a.EventID = t.EventIDWHERE t.EventScheduled = 'True' AND a.Status = 'True'GROUP BY t.EventName, t.EventDate, t.Location the only question on the coalesce, does it care how many records there will be, since it will be different for each tourney |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-30 : 18:32:46
|
| The recursive nature of the variable will iterate over every row returned from the select. The coalesce function allows the variable to retain each value and thus creates the comma delimited list. ( I hope I am making sense. )In short it doesnt matter how many player names are returned to the variable. The only governing issue is how large of a varchar value you declare it as. |
 |
|
|
fizban2
Starting Member
4 Posts |
Posted - 2008-02-13 : 21:02:18
|
quote: Originally posted by jdaman The recursive nature of the variable will iterate over every row returned from the select. The coalesce function allows the variable to retain each value and thus creates the comma delimited list. ( I hope I am making sense. )In short it doesnt matter how many player names are returned to the variable. The only governing issue is how large of a varchar value you declare it as.
Another problem i ran into, the query works, but i need to output the data from SQL to a SAP webpage, should this be converted to a stored procedure? |
 |
|
|
|
|
|
|
|