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 2005 Forums
 Transact-SQL (2005)
 Retrivieng multiple users for a single event

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.LastName
FROM 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?
Go to Top of Page

fizban2
Starting Member

4 Posts

Posted - 2008-01-30 : 17:28:05
Sure thing

-Availability-
PlayerID(FK)
EventID(FK)
Status(bit)

-Players-
PlayerID(PK)
FirstName
LastName
UserName

-Tournaments-
EventID(pk)
EventName
EventDate
EventScheduled
EventPlayed
Location



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 White


This 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.EventID

WHERE t.EventScheduled = 'True' AND a.Status = 'TRUE'







Go to Top of Page

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' UNION
SELECT 1, 2, 'False' UNION
SELECT 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' UNION
SELECT 2, 'John', 'Doe', 'jDoe' UNION
SELECT 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' UNION
SELECT 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.EventID
WHERE t.EventScheduled = 'True'
AND a.Status = 'True'

SELECT t.EventName,
t.EventDate,
t.Location,
@PlayerList AS PlayerList
FROM @Availability AS a
INNER JOIN @Players AS p ON a.PlayerID = p.PlayerID
INNER JOIN @Event AS t ON a.EventID = t.EventID
WHERE t.EventScheduled = 'True'
AND a.Status = 'True'
GROUP BY t.EventName,
t.EventDate,
t.Location
Go to Top of Page

fizban2
Starting Member

4 Posts

Posted - 2008-01-30 : 18:05:51
i changed it around a little bit and got it to work


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 Tournaments AS t ON a.EventID = t.EventID
WHERE t.EventScheduled = 'True'
AND a.Status = 'True'

SELECT t.EventName,
t.EventDate,
t.Location,
@PlayerList AS PlayerList
FROM Availability AS a
INNER JOIN Players AS p ON a.PlayerID = p.PlayerID
INNER JOIN Tournaments AS t ON a.EventID = t.EventID
WHERE 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
Go to Top of Page

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.
Go to Top of Page

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?
Go to Top of Page
   

- Advertisement -