koshi
Legacy Member
De afgelopen weken heb ik mij een beetje verdiept in SQL, maar mijn kennis rijkt nog niet ver genoeg om mijn probleem op te lossen.
Ik wil de stand berekenen van gespeelde wedstrijden maar bij berekenen telt hij ook de nog niet gespeelde wedstrijden mee op.
Hoe kan ik dit oplossen?
TABLE teams(id int primary key auto_increment,teamname char(32
Teams
+----+-----------+
| id | teamname |
+----+-----------+
| 1 | ploeg1 |
| 2 | ploeg2 |
| 3 | ploeg3 |
| 4 | ploeg4 |
+----+-----------+
TABLE matches(matchid int primary key auto_increment, date datetime,
hometeam int , awayteam int, homescore tinyint,awayscore tinyint);
+--+----------------------+-----------+----------+----------+----------+
|id | datum |hometeam |awayteam |homescore|awayscore |
+--+----------------------+-----------+----------+----------+----------+
| 1 | 2015-01-01 20:00:00 | 1 | 2 | 1 | 0 |
| 2 | 2015-01-01 20:00:00 | 3 | 4 | 0 | 2 |
| 3 | 2015-01-08 20:00:00 | 1 | 3 | 1 | 1 |
| 4 | 2015-01-08 20:00:00 | 2 | 4 | 2 | 1 |
| 5 | 2015-01-08 20:00:00 | 1 | 4 | NULL | NULL |
| 6 | 2015-01-08 20:00:00 | 2 | 3 | NULL | NULL |
| 7 | 2015-01-08 20:00:00 | 1 | 4 | NULL | NULL |
| 8 | 2015-01-08 20:00:00 | 2 | 3 | NULL | NULL |
| 9 | 2015-01-08 20:00:00 | 1 | 4 | NULL | NULL |
|10| 2015-01-08 20:00:00 | 2 | 3 | NULL | NULL |
|11| 2015-01-08 20:00:00 | 1 | 4 | NULL | NULL |
|12| 2015-01-08 20:00:00 | 2 | 3 | NULL | NULL |
+--+----------------------+--------+--------+---------+---------+
SELECT
teamname AS Ploeg, Sum(Gesp) AS Gesp, Sum(Gew) AS Gew, Sum(Gel) AS Gel, Sum(Verl) AS Verl,
SUM(Dvoor) as Dvoor, SUM(Dtegen) AS Dtegen, SUM(Versch) AS Versch, SUM(Ptn) AS Ptn
FROM(
SELECT
hometeam Ploeg,
1 Gesp,
IF(homescore > awayscore,1,0) Gew,
IF(homescore = awayscore,1,0) Gel,
IF(homescore < awayscore,1,0) Verl,
homescore Dvoor,
awayscore Dtegen,
homescore-awayscore Versch,
CASE WHEN homescore > awayscore THEN 3 WHEN homescore = awayscore THEN 1 ELSE 0 END PTN
FROM matches
UNION ALL
SELECT
awayteam,
1,
IF(homescore < awayscore,1,0),
IF(homescore = awayscore,1,0),
IF(homescore > awayscore,1,0),
awayscore,
homescore,
awayscore-homescore Versch,
CASE WHEN homescore < awayscore THEN 3 WHEN homescore = awayscore THEN 1 ELSE 0 END
FROM matches
) as tot
JOIN teams t ON tot.Ploeg=t.id
GROUP BY Ploeg
ORDER BY SUM(ptn) DESC;
Hier wordt bij gesp. de nog niet gespeelde wedstrijden bijgeteld.
+-----------+-------+-------+-------+-------+-------+-----------+-----------+---+
|Ploeg |Gesp |Gew |Gel |Verl |Dvoor |Dtegen |Versch |Ptn|
+-----------+-------+-------+-------+-------+-------+-----------+-----------+---+
|ploeg1 |6 |1 |1 |0 |2 |1 |1 |4 |
|ploeg4 |6 |1 |0 |1 |3 |2 |1 |3 |
|ploeg2 |6 |1 |0 |1 |2 |2 |0 |3 |
|ploeg3 |6 |0 |1 |1 |1 |3 |-2 |1 |
+-----------+-------+-------+-------+-------+-------+-----------+-----------+---+
Ik wil de stand berekenen van gespeelde wedstrijden maar bij berekenen telt hij ook de nog niet gespeelde wedstrijden mee op.
Hoe kan ik dit oplossen?
TABLE teams(id int primary key auto_increment,teamname char(32
Teams
+----+-----------+
| id | teamname |
+----+-----------+
| 1 | ploeg1 |
| 2 | ploeg2 |
| 3 | ploeg3 |
| 4 | ploeg4 |
+----+-----------+
TABLE matches(matchid int primary key auto_increment, date datetime,
hometeam int , awayteam int, homescore tinyint,awayscore tinyint);
+--+----------------------+-----------+----------+----------+----------+
|id | datum |hometeam |awayteam |homescore|awayscore |
+--+----------------------+-----------+----------+----------+----------+
| 1 | 2015-01-01 20:00:00 | 1 | 2 | 1 | 0 |
| 2 | 2015-01-01 20:00:00 | 3 | 4 | 0 | 2 |
| 3 | 2015-01-08 20:00:00 | 1 | 3 | 1 | 1 |
| 4 | 2015-01-08 20:00:00 | 2 | 4 | 2 | 1 |
| 5 | 2015-01-08 20:00:00 | 1 | 4 | NULL | NULL |
| 6 | 2015-01-08 20:00:00 | 2 | 3 | NULL | NULL |
| 7 | 2015-01-08 20:00:00 | 1 | 4 | NULL | NULL |
| 8 | 2015-01-08 20:00:00 | 2 | 3 | NULL | NULL |
| 9 | 2015-01-08 20:00:00 | 1 | 4 | NULL | NULL |
|10| 2015-01-08 20:00:00 | 2 | 3 | NULL | NULL |
|11| 2015-01-08 20:00:00 | 1 | 4 | NULL | NULL |
|12| 2015-01-08 20:00:00 | 2 | 3 | NULL | NULL |
+--+----------------------+--------+--------+---------+---------+
SELECT
teamname AS Ploeg, Sum(Gesp) AS Gesp, Sum(Gew) AS Gew, Sum(Gel) AS Gel, Sum(Verl) AS Verl,
SUM(Dvoor) as Dvoor, SUM(Dtegen) AS Dtegen, SUM(Versch) AS Versch, SUM(Ptn) AS Ptn
FROM(
SELECT
hometeam Ploeg,
1 Gesp,
IF(homescore > awayscore,1,0) Gew,
IF(homescore = awayscore,1,0) Gel,
IF(homescore < awayscore,1,0) Verl,
homescore Dvoor,
awayscore Dtegen,
homescore-awayscore Versch,
CASE WHEN homescore > awayscore THEN 3 WHEN homescore = awayscore THEN 1 ELSE 0 END PTN
FROM matches
UNION ALL
SELECT
awayteam,
1,
IF(homescore < awayscore,1,0),
IF(homescore = awayscore,1,0),
IF(homescore > awayscore,1,0),
awayscore,
homescore,
awayscore-homescore Versch,
CASE WHEN homescore < awayscore THEN 3 WHEN homescore = awayscore THEN 1 ELSE 0 END
FROM matches
) as tot
JOIN teams t ON tot.Ploeg=t.id
GROUP BY Ploeg
ORDER BY SUM(ptn) DESC;
Hier wordt bij gesp. de nog niet gespeelde wedstrijden bijgeteld.
+-----------+-------+-------+-------+-------+-------+-----------+-----------+---+
|Ploeg |Gesp |Gew |Gel |Verl |Dvoor |Dtegen |Versch |Ptn|
+-----------+-------+-------+-------+-------+-------+-----------+-----------+---+
|ploeg1 |6 |1 |1 |0 |2 |1 |1 |4 |
|ploeg4 |6 |1 |0 |1 |3 |2 |1 |3 |
|ploeg2 |6 |1 |0 |1 |2 |2 |0 |3 |
|ploeg3 |6 |0 |1 |1 |1 |3 |-2 |1 |
+-----------+-------+-------+-------+-------+-------+-----------+-----------+---+