Is there a more elegant way of doing this:
SELECT TOP (@NumOfGames)
(SUM(IIF(Fixture.HomeTeamID = @Team
,IIF(Fixture.Goals.FullTimeHomeGoals > Fixture.Goals.FullTimeAwayGoals
,1
,IIF(Fixture.Goals.FullTimeHomeGoals < Fixture.Goals.FullTimeAwayGoals, 0, 0.5)
) --IsHomeTeam
,IIF(Fixture.Goals.FullTimeAwayGoals > Fixture.Goals.FullTimeHomeGoals
,1
,IIF(Fixture.Goals.FullTimeAwayGoals < Fixture.Goals.FullTimeHomeGoals, 0, 0.5)
) --IsAwayTeam
)) / @NumOfGames) * 100 AS Result
FROM
Fixture.Fixture
INNER JOIN
Fixture.Goals ON Fixture.Goals.FixtureID = Fixture.Fixture.FixtureID
WHERE
HomeTeamID = @Team
OR
AwayTeamID = @Team
I hate the SUM section with a passion, there must be a better way of doing this.
User enters the @NumOfGames and @Team as parameters to the stored procedure. It then checks the goals table to see if a team won, drew or lost. A win is 1 point, a draw is 0.5 and a loss is 0. I want to output the sum of these numbers, divide them by the NumOfGames and multiply by 100 to get a success rate.