Help SQL question (Calculation)
Can someone please help. I am trying to calculate games behind but the query must be missing something (.5 games not showing up) (since not all teams always have same game count top team has played 2 games so teams that played 1 are not really 1 back they should be .5) similar
Current Results
Team GP Wins Losses Points PCT GB
A 2 2 0 4 1.000 0.0
B 1 1 0 2 1.000 0.0-Should be 0.5 (halfback)
C 1 1 0 2 1.000 0.0-Should be 0.5 (halfback)
D 1 1 0 2 1.000 0.0-Should be 0.5 (halfback)
E 1 0 1 1 0.000 1.0
F 1 0 1 1 0.000 1.0
G 1 0 1 1 0.000 1.0
H 2 0 2 2 0.000 2.0
My Code
SELECT
Team,
COUNT(*) AS GP,
SUM(is_win) AS Wins,
SUM(NOT is_win) AS Losses,
2 * SUM(is_win) + SUM(NOT is_win) AS Points,
truncate(SUM(is_win) / ((SUM(is_win)+(sum(NOT is_win)))), 3) AS PCT,
truncate((case when Count(*) - (sum(is_win) - sum(NOT is_win)) = 0 then 0
else (abs(Count(*) - (sum(is_win) - sum(NOT is_win))) / 2) end),1)as GB
FROM
(
SELECT
HomeTeam AS Team,
HomeScore > AwayScore AS is_win
FROM Games
UNION ALL
SELECT
AwayTeam AS Team,
HomeScore < AwayScore AS is_win
FROM Games
) T1
GROUP BY Team
ORDER BY Wins DESC, PCT DESC, Losses ASC
Subject
Written By
Posted
Help SQL question (Calculation)
August 15, 2014 10:31AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.