我有多个表,他们通过NAME链接。
Table PlayerDetails:
PlayerName | Gender | Grade
Mike | Male | 3rd
Brian | Male | 4th
Audrey | Female | 3rd
Karen | Female | 5th
Steve | Male | 4th
Table PlayerEval:
PlayerName | Skill-1 | Skill-2 | Skill-3 | Year
Mike | 3 | 2 | 4 | 2014
Mike | 3.5 | 1 | 2 | 2014
Mike | | 1 | | 2014
Brian | 3 | 2 | 4 | 2014
Karen | 3 | | 4 | 2014
Karen | | 2 | 4 | 2014
Audrey | 3 | 2 | | 2013
Mike | | 2 | 4 | 2013
Table CoachEval:
PlayerName | Skill-1 | Skill-2 | Skill-3 | Year
Mike | 2 | 1 | 3 | 2014
Brian | 3 | 2 | 4 | 2014
Karen | 3 | 2 | 4 | 2014
Karen | 3 | 2 | 4 | 2013
Audrey | 3 | 2 | 4 | 2013
Table TeamInfo:
PlayerName | Team | Year
Mike | Team1 | 2014
Mike | Team2 | 2013
Brian | Team1 | 2014
Karen | Team3 | 2014
关键点是我想在PlayerEval和CoachEval中为PlayerDetails表中的所有名称求和。玩家可能存在也可能不存在于PlayerEval或CoachEval表中,或者它们可能存在,但前几年 - 我只想要2014年的值。 还显示团队信息(如果存在)。我写了一个让我接近的程序,但我通常总是错过一些数据。
上表中的技能在下面的查询中有所不同。示例Skill_1是BallControl_dribbling,Skill_2是1v1_dribbling等等......
谢谢,麦克
Procedure:
declare y int;
declare g int;
declare gender varchar(20);
set y = 1;
set g = 1;
SELECT @x := count(*) from AgeGroups; # this table just includes a list of ages[U9,U10,U11,U12,U13,U14]
while g < 3 DO
if (g = 1) then
set gender = 'Male';
else
set gender = 'FeMale';
end if;
while y < 8 DO
SELECT @grade := Grade from AgeGroups where id=y;
SELECT @v3 := Age from AgeGroups where id=y;
SELECT @TotalRegistered := count(*) from PlayerDetails WHERE PlayerDetails.Grade = @grade AND PlayerDetails.Gender=gender;
SELECT PlayerDetails.id,PlayerDetails.PlayerName, PlayerEvals.PlayerName, TeamInfo.NAME,
PlayerDetails.Gender, PlayerDetails.Grade,
round(sum( ifnull( `PlayerEvals`.`BallControl_dribbling` , 0 ) + ifnull(`PlayerEvals`.`1v1_dribbling` , 0 ) + ifnull( `PlayerEvals`.`1_Touch_No-pressure_receiving` , 0 ) + ifnull( `PlayerEvals`.`1_Touch_with-pressure_receiving` , 0 ) + ifnull( `PlayerEvals`.`Air_No-pressure_receiving` , 0 ) + ifnull( `PlayerEvals`.`Air_with-pressure_receiving` , 0 ) + ifnull( `PlayerEvals`.`weighted_pass_passing` , 0 ) + ifnull( `PlayerEvals`.`combination_play_passing` , 0 ) + ifnull( `PlayerEvals`.`crossing_passing` , 0 ) + ifnull( `PlayerEvals`.`chipping_passing` , 0 ) + ifnull( `PlayerEvals`.`placement_shooting` , 0 ) + ifnull( `PlayerEvals`.`awareness_shooting` , 0 ) + ifnull( `PlayerEvals`.`1v1_defense` , 0 ) + ifnull( `PlayerEvals`.`2v2_defense` , 0 ) ) / ( count( `PlayerEvals`.`BallControl_dribbling` ) + count( `PlayerEvals`.`1v1_dribbling` ) + count( `PlayerEvals`.`1_Touch_No-pressure_receiving` ) + count( `PlayerEvals`.`1_Touch_with-pressure_receiving` ) + count( `PlayerEvals`.`Air_No-pressure_receiving` ) + count( `PlayerEvals`.`Air_with-pressure_receiving` ) + count( `PlayerEvals`.`weighted_pass_passing` ) + count( `PlayerEvals`.`combination_play_passing` ) + count( `PlayerEvals`.`crossing_passing` ) + count( `PlayerEvals`.`chipping_passing` ) + count( `PlayerEvals`.`placement_shooting` ) + count( `PlayerEvals`.`awareness_shooting` ) + count( `PlayerEvals`.`1v1_defense` ) + count( `PlayerEvals`.`2v2_defense` ) ),4) AS Tryout_Average,
round(sum( ifnull( `CoachEvals`.`BallControl_dribbling` , 0 ) + ifnull( `CoachEvals`.`1v1_dribbling` , 0 ) + ifnull( `CoachEvals`.`1_Touch_No-pressure_receiving` , 0 ) + ifnull( `CoachEvals`.`1_Touch_with-pressure_receiving` , 0 ) + ifnull( `CoachEvals`.`Air_No-pressure_receiving` , 0 ) + ifnull( `CoachEvals`.`Air_with-pressure_receiving` , 0 ) + ifnull( `CoachEvals`.`weighted_pass_passing` , 0 ) + ifnull( `CoachEvals`.`combination_play_passing` , 0 ) + ifnull( `CoachEvals`.`crossing_passing` , 0 ) + ifnull( `CoachEvals`.`chipping_passing` , 0 ) + ifnull( `CoachEvals`.`placement_shooting` , 0 ) + ifnull( `CoachEvals`.`awareness_shooting` , 0 ) + ifnull( `CoachEvals`.`1v1_defense` , 0 ) + ifnull( `CoachEvals`.`2v2_defense` , 0 ) + ifnull( `CoachEvals`.`offense_heading`, 0 ) + ifnull( `CoachEvals`.`defense_heading` , 0 ) + ifnull( `CoachEvals`.`5v5_teamdefense` , 0 ) ) / ( count( `CoachEvals`.`BallControl_dribbling` ) + count( `CoachEvals`.`1v1_dribbling` ) + count( `CoachEvals`.`1_Touch_No-pressure_receiving` ) + count( `CoachEvals`.`1_Touch_with-pressure_receiving` ) + count( `CoachEvals`.`Air_No-pressure_receiving` ) + count( `CoachEvals`.`Air_with-pressure_receiving` ) + count( `CoachEvals`.`weighted_pass_passing` ) + count( `CoachEvals`.`combination_play_passing` ) + count( `CoachEvals`.`crossing_passing` ) + count( `CoachEvals`.`chipping_passing` ) + count( `CoachEvals`.`placement_shooting` ) + count( `CoachEvals`.`awareness_shooting` ) + count( `CoachEvals`.`1v1_defense` ) + count( `CoachEvals`.`2v2_defense` ) +count(`CoachEvals`.`offense_heading`) +count(`CoachEvals`.`defense_heading`) + count( `CoachEvals`.`5v5_teamdefense`) + count(`CoachEvals`.`offense_heading`) + count(`CoachEvals`.`defense_heading`) ),4) AS Coach_Average,
round(sum( ifnull( `CoachEvals`.`GeneralAttitude`, 0)) / ( count(`CoachEvals`.`GeneralAttitude`) ),4) AS Coach_General_Attitude,
round(sum( ifnull( `CoachEvals`.`AttendsPractice`, 0)) / ( count(`CoachEvals`.`AttendsPractice`) ),4) AS Coach_Attends_Practice,
round(sum( ifnull( `CoachEvals`.`AbilitytoLearn`, 0)) / ( count(`CoachEvals`.`AbilitytoLearn`) ),4) AS Coach_Ability_to_Learn,
round(sum( ifnull( `CoachEvals`.`Goalie`, 0) ) / ( count(`CoachEvals`.`Goalie`) ),4) AS Coach_Goalie
FROM `PlayerDetails`
LEFT JOIN PlayerEvals ON PlayerDetails.PlayerName=PlayerEvals.PlayerName
AND (PlayerEvals.Date=@try1 OR PlayerEvals.Date=@try2)
LEFT JOIN CoachEvals ON (PlayerDetails.PlayerName=CoachEvals.PlayerName)
LEFT JOIN TeamInfo ON (PlayerDetails.PlayerName = TeamInfo.PlayerName)
WHERE PlayerEvals.Date is NULL or (PlayerEvals.Date=@try1 OR PlayerEvals.Date=@try2)
AND PlayerDetails.Gender = gender
AND PlayerDetails.Grade = @grade
group by PlayerDetails.Gender,PlayerDetails.PlayerName
order by Tryout_Average;
set y = y + 1;
END WHILE;
set g = g + 1;
set y = 1;
END WHILE;
END
预期产出:
Playername TEAM NAME Gender Grade Tryout_Average Coach_Average
Mike Team1 Male 3rd Grade 2.07 3
Brian NULL Male 4th Grade 3 3
Steve NULL Male 4th Grade NULL NULL
Audrey NULL Female 3rd Grade NULL NULL
Karen NULL Female 5th Grade 3.25 3
在查询中计算Tryout_Average和Coach_Average的平均值。如果它们不是NULL,我使用Skill-1,Skill-2,Skill-3的总和,然后除以值的计数。