I have this rather large Query in which I select Players (hockey players), their team, and their stats (GP, G, A, TP)
during a certain season.
The thing is that during a season a player could play for
more than one team, which means that two seperate
rows could be returned: one with the first team he
played for and one row with the second team he played for.
One thing that makes this a tad more difficult is that the
STATS are all in VARCHAR, so I must lpad them to get
them ordered in the correct way.
Anyhow, here is my QUERY:
$rookie = "SELECT player.PlayerID, player.Firstname, player.Lastname, team.TeamID,
team.Team, stats.PlayerID, stats.KID, MAX(stats.TeamID), stats.GP, stats.G, stats.A, stats.TP
LPAD(stats.GP,2,'0') as GP, LPAD(stats.G,2,'0') as G, LPAD(stats.A,2,'0') as A, (stats.TP,3,'0') as TP,
SUM(GP), SUM(G), SUM(A), SUM(TP)
FROM player INNER JOIN stats ON stats.TeamID=team.TeamID
INNER JOIN team ON player.PlayerID=stats.PlayerID
WHERE stats.League='NHL' AND stats.EndDate='$Year' AND player.NationID='1' AND player.POS <> 'G'
GROUP BY stats.PlayerID
ORDER BY TP DESC, G DESC, GP ASC";
So what is wrong with it? Well, it only returns one player (so the GROUP BY works) but it doesn't SUM the values.
-------EXAMPLE------
If you want it visualised here's how the page currently looks (without GROUP BY and SUM)
http://www.eliteprospects.com/scoring6.php?sort=forwards&Year=1996
Look for player "Markus Näslund" and you'll see that he is listed at #6 as well as #15. I want those two to be added together and listed as one.