I am working on a stats site for softball. I have been collecting data for a while and need to present a "season" view, per player, and a "tournament" view, per player.Each player is assigned a teamid, and every statistical record carries some identifying information:id = id for stat rowteamid = id of team player plays forgameid = id of the game the player is playing intourneyid = id of the tournament to which the game belongsseason = the season, ie 2009, 2010, etc.For a game, a player will only have 1 stat row. However, for a tournament, the player will 4 - 10 stat rows, and obviously for a season, there could be 100 or more.What I want to do is this. I need a query that will query the player table for all of the playerid's for a given teamid, then for each player id, query they stats table and perform stats calculations. I already have the stats calculations set up on a per game basis, so that is cool. I just need the part that gets the playerid from the player table and uses that as an input for the query on the stats table.Something like:Select playerid from playertable where teamid=5Loop through playerid's that were returned and do another query on the stats table.return the dataset of the second set of queries and calculations.
Below is the query for the per game stats:SELECT p.id as 'ID', p.playerlastname as 'Name', o.innings as 'I', o.atbats as 'AB', o.runs as 'R', o.hits as 'H', o.singles as '1B', o.doubles as '2B', o.triples as '3B', o.homeruns as 'HR', o.runsbattedin as 'RBI', o.baseonballs as 'BB', o.strikeouts as 'K', o.sacrificeflys as 'SF', o.sacrificehits as 'SH', o.stolenbases as 'SB', o.errors as 'E', cast(cast(o.hits +o.baseonballs as decimal) / case when o.atbats+o.baseonballs+o.sacrificeflys+o.sacrificehits = 0 then 1 else cast(o.atbats+o.baseonballs+o.sacrificeflys+o.sacrificehits as decimal) end as decimal(4,3)) as 'OBP', cast(cast(o.singles+(o.doubles*2)+(o.triples*3)+(o.homeruns*4)as decimal) / case when o.atbats= 0 then 1 else cast(o.atbats as decimal(4,3)) end as decimal(4,3)) as 'SLG', cast(cast(o.hits as decimal) / case when o.atbats=0 then 1 else cast(o.atbats as decimal(4,3)) end as decimal(4,3)) as 'AVG'from player as p left join offensivestats as o on p.id=o.playeridWhere o.gameid=@idorder by p.playerlastname