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
 General SQL Server Forums
 New to SQL Server Programming
 Help with Top Scorers SQL statement

Author  Topic 

stark8352
Starting Member

6 Posts

Posted - 2011-08-26 : 13:54:13
Hi Guys,

I'm new to this forum and have a question about an sql statement I have that creates a list of the top scorers (with their team name). My only problem is that some players may have played for more than one team so when the sql statement is run it shows those players' goals split by team. Is there anyway to amend my statement so that the player only gets listed once with all of their goals and with either the last team he played for or some kind of concatentaion of the teams they've played for. For example:

Bent scored 17 goals during 2010/11 but 8 were for Sunderland and 9 for Aston Villa. Whereas my current sql statement shows his entry twice I would like to show something like:

Option 1: Bent 17 Aston Villa
Option 2: Bent 17 Aston Villa/Sunderland

My current statement is a follows:

SELECT TOP (10) Scorers.Player, Teams.TeamName, COUNT(Scorers.Player) AS TG
FROM Scorers INNER JOIN Teams ON Scorers.TeamID = Teams.TeamID
GROUP BY Scorers.Player, Teams.TeamName
ORDER BY TG DESC, Scorers.Player


Any help would be appreciated

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-08-26 : 14:28:07
There's probably a better way to do this, but this is what I've done:

(note, I have 3 tables involved: Players, Scores, Teams)

Here's my main query, which calls a function below to get the team(s)
Declare @numRecs int
set @numRecs = 50

select TOP (@numRecs) WITH TIES rank() over(order by sum(points) desc) rank, first + ' ' + last player, pos, sum(points) pts,
dbo.ptsByTeam(id) team
from players p
join scores s
on p.id = s.playerid
group by
id, first, last, pos
order by
[rank]


The function:
CREATE Function [dbo].[ptsByTeam] (
@playerId int
)
Returns varchar(255)
AS
BEGIN

Declare @team varchar(32)
Declare @points int
Declare @str varchar(255)
Declare @tm varchar(32)
set @str = ''

Declare c Cursor
read_only
for
select t.team, sum(points) pts
from scores s
join teams t
on t.id = s.team
where playerid = @playerId
group by
t.team
order by pts desc
Open c
fetch next from c into @team, @points
set @tm = @team

while @@fetch_status = 0
begin
if @points <> 0
begin
set @str = @str + @team + ' (' + convert(varchar(5), @points) + '), '
end
fetch next from c into @team, @points
end

Close c
Deallocate c

If len(@str) > 1
begin
set @str = left(@str, len(@str)-1)
end
If charindex(',', @str) = 0
begin
return @tm
end

if @str is null
begin
return @tm
end

Return ltrim(rtrim(@str))
END
Go to Top of Page

stark8352
Starting Member

6 Posts

Posted - 2011-08-26 : 16:00:58
Russell,

Thanks for the quick response. As a newbie to SQL aswell, I will have to take some time to digest your code but thanks for pointing me in the right direction.

Steve
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-27 : 04:38:45
isnt this enough?


SELECT Player,TG,
STUFF((SELECT ',' + TeamName FROM Teams WHERE TeamID = t.TeamID ORDER BY TeamID ASC FOR XML PATH('')),1,1,'') AS TeamList
FROM
(
SELECT Scorers.Player,COUNT(Scorers.Player) AS TG
FROM Scorers
GROUP BY Scorers.Player
)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

stark8352
Starting Member

6 Posts

Posted - 2011-09-01 : 13:47:15
visakh16,

Thanks for the additional post. After some fiddling about with the code, I eventually cracked it. This is what I ended up with:

SELECT TOP (10) Player, COUNT(Player) AS Expr1, STUFF
((SELECT TOP (100) PERCENT '/' + a.TeamName AS [text()]
FROM Teams AS a INNER JOIN Scorers AS t ON a.TeamID = t.TeamID
WHERE (t.Player = b.Player) GROUP BY a.TeamName
ORDER BY a.TeamName FOR XML PATH('')), 1, 1, '') AS TeamList
FROM Scorers AS b
GROUP BY Player
ORDER BY Expr1 DESC

Thanks again

Steve
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-01 : 14:02:33
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -