Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query calculated also not played games

Author  Topic 

koshi
Starting Member

1 Post

Posted - 2015-03-01 : 17:13:10
Hi, I'm new here and I have very little knowledge of SQL.
I was searching for a SQL query to calculate a soccer table
So I found it at https://jonlabelle.com/snippets/view/sql/league-table-in-mysql.

The query works fine, only when I add games for the future then he calculate them as played. How can I fix this?
Thx for your help.

SELECT
tname AS Team, Sum(P) AS P,Sum(W) AS W,Sum(D) AS D,Sum(L) AS L,
SUM(F) as F,SUM(A) AS A,SUM(GD) AS GD,SUM(Pts) AS Pts
FROM(
SELECT
hteam Team,
1 P,
IF(hscore > ascore,1,0) W,
IF(hscore = ascore,1,0) D,
IF(hscore < ascore,1,0) L,
hscore F,
ascore A,
hscore-ascore GD,
CASE WHEN hscore > ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END PTS
FROM games
UNION ALL
SELECT
ateam,
1,
IF(hscore < ascore,1,0),
IF(hscore = ascore,1,0),
IF(hscore > ascore,1,0),
ascore,
hscore,
ascore-hscore GD,
CASE WHEN hscore < ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END
FROM games
) as tot
JOIN teams t ON tot.Team=t.id
GROUP BY Team
ORDER BY SUM(Pts) DESC ;

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-02 : 04:12:31
is there a flag in the games table that indicates it is complete or a game date column?
Go to Top of Page
   

- Advertisement -