I have table 'games' and want to select the highest win and loss of the team I want.
I have table like this:
ID team1 team2 score1 score2
1 KVA PLZ 8 5
2 MLB KVA 0 8
3 PLZ SPA 0 6
4 SPA MLB 5 2
5 KVA SPA 3 5
6 PLZ MLB 7 1
Two examples:
1) I want to select the highest win of team 'KVA', then I want to get this row:
ID team1 team2 score1 score2
2 MLB KVA 0 8
2) I want to select the highest loss of team 'PLZ', then I want to get:
ID team1 team2 score1 score2
3 PLZ SPA 0 6
Requirements:
Winning 10:0 is better than 11:1
Losing 1:11 is better than 0:10
Thank you for any help.
The basic approach is to sort by difference in scores and then a second column for tie-breakers. It's slightly complicated because the team can be in either of the team columns.
Highest Win of KVA:
Select
*
From
games
Where
team1 = 'KVA' Or
team2 = 'KVA'
Order By
Case When team1 = 'KVA' Then score1 - score2 else score2 - score1 end Desc,
Case When team1 = 'KVA' then score1 else score2 end
Limit 1
Highest Loss of PLZ
Select
*
From
games
Where
team1 = 'PLZ' Or
team2 = 'PLZ'
Order By
Case team1 when 'PLZ' Then score2 - score1 else score1 - score2 end Desc,
Case team1 when 'PLZ' then score2 else score1 end
Limit 1
Example Fiddle
Related
I'm trying to compare two soccer players who played in the same team and I'm trying to know who scored more goals in each game they played together and COUNT the number of times a player scored more goals than the other in each game. For example:
Game 1: Messi 2 goals - Neymar 0 goals
Game 2: Messi 2 goals - Neymar 3 goals
Game 3: Messi 4 goals - Neymar 1 goal
The final result should be Messi = 2 , because he scored more goals in 2 games.
I have the next query to find the players who share the same team and game with my chosen player (Messi in this example):
SELECT S1.Team, S1.Game, S1.Player, S2.Team, S2.Game, S2.Player
FROM Mytable S1
INNER JOIN Mytable S2 ON S1.Team = S2.Team AND
S1.Game= S2.Game AND
S1.Player LIKE '".mysql_real_escape_string($Messiinthiscase)."'
AND S2.Player <> '".mysql_real_escape_string($Messiinthiscase)."';
Mytable is like:
Player | Team | Game | Goals
-------------------------------
Messi A G1 2
Neymar A G1 0
Messi A G2 2
Neymar A G2 3
Messi A G3 4
Neymar A G3 1
but I don't know how to implement a COUNT to compare both players. Probably it is a stupid question with an easy answer but I've been hours working on it and nothing comes to my mind.
Thanks for all your help
Although a strange query in itself, lets see if this helps. You are on a good track, and as you stated, it is another TEAM-MATE who PLAYED IN the same game... So you need a SUM() based on a case/when grouped by your "Messi" player and whoever else played and scored... THEN apply a HAVING. So, if Messi had 5 team-mates, there should be 4 possible returns
Messi vs Team-mate 1
Messi vs Team-mate 2
Messi vs Team-mate 3
Messi vs Team-mate 4
and of those, Messi may have scored more than team-mates 1, 2 and 4, but less than 3.
SELECT
S1.Team,
S1.Player,
S2.Player,
sum( case when s1.goals > s2.goals then 1 else 0 end ) as Player1Higher,
sum( case when s1.goals < s2.goals then 1 else 0 end ) as Player1Lower
FROM
Mytable S1
INNER JOIN Mytable S2
ON S1.Team = S2.Team
AND S1.Game = S2.Game
AND NOT S1.Player = S2.Player
WHERE
S1.Player LIKE '".mysql_real_escape_string($Messiinthiscase)."'
GROUP BY
S1.Team,
S1.Player,
S2.Player
HAVING
sum( case when s1.goals > s2.goals then 1 else 0 end ) >
sum( case when s1.goals < s2.goals then 1 else 0 end )
Notice the WHERE clause has your criteria for "Messi" string, and the JOIN clause just says player 1 (Messi) IS NOT the same for player 2.
The group by is NOT including the EACH GAME as you want the total based on games that the players both played in...
Finally the having includes the final "keep only these records" where the sum of player 1 goals exceeds the sum of player 2 goals.
If you remove the HAVING clause, you would see your results that Messi had 2 games higher goals and 1 game LESS goals compared to player Neymar
The comparisons do NOT consider "=" as neither player had MORE points than the other... so if such a game where BOTH people scored 2 points, then neither would be considered the higher scoring player.
The comparison could be simplified even more by doing
sum( case when s1.goals = s2.goals then 0
when s1.goals > s2.goals then 1 else -1 end ) as NetGames
So, again, if the score is tied, then nobody is better. If s1 goals are higher, +1, otherwise the other player was higher, -1. If the Net Games is positive, then player 1 was better player, = 0 tie, negative is other player better.
SELECT S1.Team, S1.Game, S1.Player, S2.Team, S2.Game, S2.Player, CASE WHEN S1.goals < S2.goals THEN S2.goals - S1.goals WHEN S2.goals < S1.goals THEN S1.goals - S2.goals ELSE 0 END as difference
FROM Mytable S1
INNER JOIN Mytable S2 ON S1.Team = S2.Team AND
S1.Game= S2.Game AND
S1.Player LIKE '".mysql_real_escape_string($Messiinthiscase)."'
AND S2.Player <> '".mysql_real_escape_string($Messiinthiscase)."';
I am trying to display a list of teams with the number of goals they have scored (and order them by greatest to smallest) but am having trouble with joining all the queries together
Table 1: Teams
teamid teamname
1 team1
2 team2
3 team3
Table 2: Results
id gameid teamid gf
1 1 1 5
2 2 1 3
3 1 2 0
4 2 2 2
5 3 3 0
What I'm trying to achieve
1. Team1 8
2. Team2 2
3. Team3 0
Get list of all teams
SELECT team.teamid, team.teamname
FROM teams team
Gets sum of goals for 1 team
SELECT COALESCE( SUM( gf ) , 0 ) goalsfor
FROM results
WHERE teamid = 1
Joining of queries
SELECT team.teamid,
team.teamname,
COALESCE(res.gf, 0) goalsfor
FROM teams team
LEFT JOIN
(SELECT COALESCE(SUM(res.gf), 0) goalsfor
FROM results res
GROUP BY teamid) res ON team.teamid = res.teamid
ORDER BY goalsfor DESC
Been stuck on joining the queries all day
SELECT teams.teamname, res.goals FROM teams JOIN (
SELECT COALESCE(SUM(results.gf),0) AS goals, results.teamid AS teamid FROM results
group by results.teamid) res
ON teams.teamid=res.teamid ORDER BY goals DESC;
My solution is:
SELECT t.id,
t.name,
SUM(r.gf) goalsfor
FROM team t
LEFT JOIN
results r ON t.id = r.teamId
GROUP BY t.id
ORDER BY goalsfor DESC
My result from my dummy table:
id name goalsfor
1 Apple 8
2 Banana 2
3 Carrot 0
I don't think you need COALESCE if you made your columns have a default of 0 and cannot be null.
For one of my sports related project, I need to show the record counts of teams.
Example: "Team 1" and "Team 2" has played 5 games. "Team 1" has won 3 times and "Team 2" has won 2 times. So the output should be (3-2).
Here is the table structure with some data.
ID ---- TEAM1 ---- SCORE1 ---- TEAM2 ---- SCORE2
1 70 1 73 2
2 74 0 70 1
3 74 2 73 1
4 73 1 70 0
The output should be something like:
TEAM1 ---- TEAM2---- RECORD
70 73 2-0
74 70 0-1
74 73 1-0
NOTE:
There will be always a winner and no game can be a draw.
In the output, the combination of team1 and team2 should be unique.
SQL Fiddle : http://sqlfiddle.com/#!2/4dead/1/0
The tricky thing here is dealing with the same teams playing in home and away fixtures. This can be worked around with (lots of) case statements.
The basic approach is to reformat the data so that the team with the lowest id appears first.
select
least(homeTeam, awayTeam) team1,
greatest(homeTeam, awayTeam) team2,
sum(case when awayTeam > homeTeam
then case when homeTeamScore > awayTeamScore then 1 else 0 end
else case when homeTeamScore > awayTeamScore then 0 else 1 end
end) team1Wins,
sum(case when hometeam > awayteam
then case when homeTeamScore > awayTeamScore then 1 else 0 end
else case when homeTeamScore > awayTeamScore then 0 else 1 end
end) team2Wins
from
games
group by
least(homeTeam, awayTeam),
greatest(homeTeam, awayTeam);
SQL Fiddle
or slightly more compact, but possibly harder to understand:
select
least(homeTeam, awayTeam) team1,
greatest(homeTeam, awayTeam) team2,
sum(case sign(awayTeam - homeTeam)
when sign(homeTeamScore - awayTeamScore) then 1
else 0 end) team1Wins,
sum(case sign(awayTeam - homeTeam)
when sign(awayTeamScore - homeTeamScore) then 1
else 0 end) team2Wins
from
games
group by
least(homeTeam, awayTeam),
greatest(homeTeam, awayTeam);
SQL Fiddle
I am building a Hockey Sports score and prediction system using PHP/MySQL. Below are the system design.
I have a GAMES table where two team numbers and their score in the game is present.The columns from this table are as below.
ID ---- TEAM1 ---- SCORE1 ---- TEAM2 ---- SCORE2
1 70 1 73 2
2 74 0 70 1
3 74 0 73 0
I also have a PICKS table where the details related to user's game predictions are present. Users can guess which team will win in a game and that data is stored in this table. The columns from this table are as below. Each user can guess only once for each game.
ID ---- GAME ---- USER ---- TEAM ---- POINT
1 1 1 70 1
2 2 1 70 1
3 3 1 73 1
3 1 2 70 1
Based on the above available data, I am trying to build up the result where each user (column USER) should be awarded the points(column POINT) for each correct guess. The guess can be validated based on the scores from GAMES table. The final output should be like as below.
USER ---- POINTS ---- CORRECT GUESS COUNT ---- WRONG GUESS COUNT
1 1 1 2
2 0 0 1
The columns "CORRECT GUESS COUNT" and "WRONG GUESS COUNT" represent the total number of correct guess and wrong guess done by the user.
I have created a SQL Fiddle for the above tables with some sample data.
http://sqlfiddle.com/#!2/8d469/4/0
EDIT:
Some more inforamtion are below. It's possible that a game can be a
draw.
In that case the score will be 0 for each team. When a game is
draw, users get no points.
SELECT p.user,
SUM(IF(g.id IS NOT NULL, p.point, 0)) As points,
SUM(IF(g.id IS NOT NULL, 1, 0)) Correct,
SUM(IF(g.id IS NULL, 1, 0)) Wrong
FROM Games g
RIGHT JOIN Picks p ON g.id = p.game AND
p.team = IF(g.score1 > g.score2 , g.team1, IF(g.score1 < g.score2, g.team2, NULL))
GROUP BY p.user;
SQL Fiddle (with your data)
You'll have to forgive me, if there is a more MySQL way to do it than this (background is Oracle/SQL Server):
SELECT
p.user
,sum(CASE
WHEN p.team = g.winner THEN point ELSE 0 END) points
,sum(CASE
WHEN p.team = g.winner THEN 1 ELSE 0 END) good_guess
,sum(CASE
WHEN p.team <> g.winner THEN 1 ELSE 0 END) bad_guess
FROM
picks p
INNER JOIN (
SELECT
id game_id
,CASE
WHEN score1 > score2 THEN team1
WHEN score2 > score1 THEN team2
ELSE -1 --no team_id as negative
END winner
FROM
games
) g
ON
g.game_id = p.game
GROUP BY
p.user
This is a bit of a complex query, but I have a database of snooker matches, and am trying to generate stats on who has played and won the most deciding frames (don't worry about the rules if you don't know the game).
Table:
ID player1 player2 bestOf player1Score player2Score
1 1 2 9 5 0
2 2 1 9 5 4
3 1 2 9 5 4
4 2 1 9 4 5
What I am trying to do is something like the following:
SELECT COUNT(*) AS played, DISTINCT(player1,player2) AS playerID
FROM matches
WHERE (player1Score=BestOf / 2 + 0.5 AND player2Score=BestOf / 2 - 0.5)
GROUP BY playerID
The above query doesn't work, as I believe DISTINCT doesn't support multiple columns. The result I'm looking for from the top table is:
playerID played won
1 3 2
2 3 1
The top row in the table is not displayed, as it is not a final frame.
I have tried variations such as:
SELECT GROUP(player1,player2)
SELECT player1 + player2 AS playerID, select DISTINCT(playerID)
SELECT (player1 + player2) AS playerID GROUP BY playerID
and a good few others. Any hint would be appreciated!
I don't actually have your tables with me so my sql probably has lots of syntax errors. But hopefully I will be to get the general idea across.
We can compute the list of deciding frame games where player1 won and union it with the list of deciding frame games where player2 won. As long as we rename the winner's column name to the same string ('player'), the union of these tables should yield a single table marking out the number of times each person won.
select player, count(*) as num_won from ((select player1 as player from matches where player1Score - player2Score = 1) union (select player2 as player from matches where player2Score - player1Score = 1));
This should give you a mapping from each player's id to the number of times they won.
Then consider
select player, count(*) as num_played from ((select player1 as player from matches where abs(player1Score - player2Score) = 1) union (select player2 as player from matches where abs(player2Score - player1Score) = 1));
The use of the abs function should help you discover the number of deciding frame games each player played. Now we combine these two tables to get your final answer
select players_table.player, players_table.num_played, winners_table.num_won from (select player, count(*) as num_won from ((select player1 as player from matches where player1Score - player2Score = 1) union (select player2 as player from matches where player2Score - player1Score = 1)) winners_table), (select player, count(*) as num_played from ((select player1 as player from matches where abs(player1Score - player2Score) = 1) union (select player2 as player from matches where abs(player2Score - player1Score) = 1)) players_table) where winners_table.player == players_table.player;
create table temp_score
select playerid, count(*) as won
FROM(
Select case when player1score > player2score then player1 else player2 end as playerid
from score
where
(player2Score=Round(BestOf / 2 + 0.5,0) AND player1Score=round(BestOf / 2 - 0.5,0))
or
(player1Score=Round(BestOf / 2 + 0.5,0) AND player2Score=round(BestOf / 2 - 0.5,0))
) a
group by playerid
Select playerid,won, (Select SUM(won) from temp_score) as TotalPlayed from temp_score