I have table like this:
home | away | home_round_1 | away_round_1 | home_round_2 | away_round_2
team1 team2 20 10 5 -2
team3 team1 1 2 -11 4
team2 team3 20 -1 -1 10
I'm trying to figure out how to calculate wins, loses, evens and average game result group by teams. Game result sit counted as home round 1 + round 2 or away round 1 + round 2 and winner is team that has less points in the end. I have made counter that count certain teams average in mysql with cases but is it possible to count all this inside mysql, that it will count values by team.
So far I have manage to count team avrage with this:
SELECT
AVG(
CASE
WHEN home = "team1" THEN home_round_1 + home_round_2
WHEN away = "team1" THEN away_round_1 + away_round_2
ELSE NULL
END
) AS 'Avrage_points'
FROM
`Game_results`
Related
Hi everyone I am new hear and I'm stuck summarizing within my SQL Database.
To make the example very simple here is my data below.
GAME_ID GOALIEID SCORED
2001 5 N
2001 4 N
2001 5 Y
2001 4 N
2001 5 Y
This is a shootout and I want to summarize the amount of saves each goalie made by Game_ID, GoalieID, and how many saves were actually made by each.
The N=SAVE AND Y=GOAL GIVEN UP
I am trying to output my result as shown below in descending order:
GAME_ID GOALIEID SAVES
2001 4 2
2001 5 1
Currently based on my code:
SELECT GAME_ID, GOALIEID, COUNT(SCORED) AS SAVES
FROM GIRAFFE.MLS
WHERE SCORED = 'N'
GROUP BY GAME_ID
ORDER BY COUNT (SCORED) DESC
But the result I am getting is just adding up all saves and attributing it to one goalie as show below.
GAME_ID GOALIEID SAVES
2001 5 3
THE ABOVE WAS RESOLVED.
The FINAL QUESTION TO FINALIZE THIs is how do you query for a STREAK in SQL
For example:
GAME_ID GOALIEID TEAMID TEAMSHOTNUM OVERALLSHOT SCORED
2001 5 1 1 1 Y
2001 4 2 1 2 N
2001 5 1 2 3 N
2001 4 2 2 4 N
2001 5 1 3 5 N
2001 4 2 3 6 N
Based on this simple example:
How can I query for the most consecutive "Saves" which would be "N" by a "GoalieID" now imagine my data has multiple games to look through but for simplicity I only have the one game here.
My result I would hope to look like this:
GAME_ID GOALIEID STREAK
2001 4 3
This would show that "GOALIEID" #4 made 3 consecutive saves to win the game showing that the highest consecutive streak of saves was in fact 3. Note remember this will be across thousands of games. But i made it simple here with just 1 game.
THANK YOU ALL FOR YOUR HELP!
Actually you should group by GAME_ID AND GOALIEID :
SELECT GAME_ID, GOALIEID, COUNT(SCORED) AS SAVES
FROM a
WHERE SCORED = 'N'
GROUP BY GAME_ID, GOALIEID
ORDER BY SAVES DESC
Let me start explaining this with an example, I have a table with records of matches played in a soccer league, by using this table and its matches results am able to generate a standings table for the teams in this league via a mysql query.
Table [matches] (example)
--------------------------------------------------------
|id | hometeam |goalsfor|goalsagainst| awayteam |
--------------------------------------------------------
--------------------------------------------------------
| 8 | Real Madrid | 2 | 0 | Inter Milan |
--------------------------------------------------------
| 9 | Inter Milan | 3 | 3 | Real Madrid |
--------------------------------------------------------
Generated standings by query
Pos Team Pld W D L F A GD Pts
1 FC Barcelona 5 2 3 0 8 5 3 9
2 Inter Milan 6 2 2 2 11 10 1 8
3 Real Madrid 6 2 2 2 8 8 0 8
4 AC Milan 5 0 3 2 8 12 -4 3
The query:
select
team,
count(*) played,
count(case when goalsfor > goalsagainst then 1 end) wins,
count(case when goalsagainst> goalsfor then 1 end) lost,
count(case when goalsfor = goalsagainst then 1 end) draws,
sum(goalsfor) goalsfor,
sum(goalsagainst) goalsagainst,
sum(goalsfor) - sum(goalsagainst) goal_diff,
sum(
case when goalsfor > goalsagainst then 3 else 0 end
+ case when goalsfor = goalsagainst then 1 else 0 end
) score
from (
select hometeam team, goalsfor, goalsagainst from scores
union all
select awayteam, goalsagainst, goalsfor from scores
) a
group by team
order by score desc, goal_diff desc;
What I want to do is to order the standings based on Head to Head matches, so it would first order by points, then if there's a draw in points the second sorting would be to look at the two teams matches and compare who has more wins, or scored more than the other, then use that to sort the table.
By doing this as in the example Real Madrid will become ranked as 2nd and then Inter Milan as 3rd.
How can I achieve this?
I want to compare the two teams matches when they are equal in points, and use that to sort.
ORDER BY score DESC, h2h DESC; goal_diff DESC
Update: I ended going with a solution mix of sql and php, first I find equaled teams in rank, and then generate mini h2h standings for those team and update the rank based on it. I still see this doable with just sql, but with my heavy query its too complicated to implement with just sql, thats why I mixed with php in the implementation.
You need to process this in two steps. First, run the query above and store the results in a work table (call it work below). Then you need to get a tie breaker score for each team that is on the same score. Below, I join the matches table to the work table for each team, and ignore any where the work rows do not have the same score, as they are not important. Then give the team 1 if they won. Have to do it again for the other side. You might want to change this to the 3 for win, 1 for draw.
Sum these results up, join that result to the team row in work, and you have a tie break score for each row where where the score is the same.
You need to check what happens if you have many teams on the same score, and see if this is the result you want.
select w.*, b.hth
From work w
left outer join (
select team, SUM(hth) hth
from (
Select hometeam team, case when m.goalsfor > m.goalsagainst then 1 else 0 end hth
from matches m
inner join work w1 on m.hometeam = w1.team
inner join work w2 on m.awayteam = w2.team
where w1.score = w2.score
union all
Select awayteam team, case when m.goalsAgainst > m.goalsFor then 1 else 0 end hth
from matches m
inner join work w1 on m.hometeam = w1.team
inner join work w2 on m.awayteam = w2.team
where w1.score = w2.score
) a --all hth at same points
group by team
) b --summed to one row per team
on b.team = w.team
order by w.score desc, b.hth desc;
I am making a website for a game as a hobby and I want to add a win percentage for every champion in it. I track every match in my DB and the outcome column can be 0 or 1 for loss or win. I am using MySql. I want my outcome table to be like this:
ChampID | Wins | Loss | Percentage
12 1 1 50%
13 2 0 100%
My Matches table looks like this:
MatchID | ChampID | PlayerID | MatchOutcome
1 12 1231 1
2 12 1414 0
3 13 1341 1
4 13 1512 1
So the champion with ID 12 will have 50% win rate while 13 will have 100%.
I have been trying unions and joins but I couldn't get them right, this is the closest I have come.
SELECT
(SELECT COUNT(*) FROM Matches WHERE (MatchOutcome LIKE "0")) AS Loss,
(SELECT COUNT(*) FROM Matches WHERE (MatchOutcome LIKE "1")) AS Win,
(SELECT (Win/(Win+Loss))*100 ) AS Percentage
This method seems the most promising as I would also like to be able to sort further by PlayerID.
Thanks for your time
Just do some simply math to do this. No reason to get creative with joins and subqueries:
SELECT sum(matchoutcome)/count(matchid) FROM Matches where PlayerID = 12;
Or, for each player in a list:
SELECT playerID, sum(matchoutcome)/count(matchid) FROM Matches GROUP BY playerID;
You can do your conversion to a percentage here too:
SELECT playerID, (sum(matchoutcome)/count(matchid))*100 FROM Matches GROUP BY playerID;
To get out Wins and Losses in separate columns:
SELECT playerID,
sum(matchOutcome) as Wins,
Count(matchid) - sum(matchOutcome) as Losses,
(sum(matchoutcome)/count(matchid))*100
FROM Matches
GROUP BY playerID;
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
I'm working on a baseball related website. I have a table with a batting lineup for two baseball teams:
+----+----------+--------------+--------+
| id | playerId | battingOrder | active |
+----+----------+--------------+--------+
Batting order is an integer between 1 and 20. This corresponds to the following logic:
Batting Order 1-9 — Away Team Lineup
Batting Order 10 — Away Team Pitcher
Batting Order 11-19 — Home Team Lineup
Batting Order 20 — Home Team Pitcher
The active field is a tinyint 0 or 1, representing the pitcher on the mound and the batter on the plate.
Known Fact: There will always be one active pitcher from one team and one active batter from the opposite team.
I need to write a query that returns a row for a home team player that corresponds to the next batter in the battingOrder. (the one that that occurs after the active batter's battingOrder)
Example:
If the player in battingOrder 13 is active, the query should return the player in batting order 14.
If the player in battingOrder 19 is active, the query should return the player in batting order 11 (the lineup loops back to the first player for the team).
I've never used a CASE query before, but I came up with the following:
SELECT *
FROM lineups
WHERE battingOrder =
CASE (
SELECT battingOrder
FROM lineups
WHERE battingOrder > 10 AND active = 1
LIMIT 1
)
WHEN 11 THEN 12
WHEN 12 THEN 13
WHEN 13 THEN 14
WHEN 14 THEN 15
WHEN 15 THEN 16
WHEN 16 THEN 17
WHEN 17 THEN 18
WHEN 18 THEN 19
WHEN 19 THEN 11
END
LIMIT 1;
It seems to work, but what edge cases and/or pitfalls have I walked into? Is this efficient? I'm particulary interested in a solution to my problem that does not use a nested query.
Select LNext.player As NextPlayer
From lineups As L
Left Join lineups As LNext
On LNext.BattingOrder Between 11 And 20
And LNext.BattingOrder = Case
When L.BattingOrder = 19 Then 11
Else L.BattingOrder + 1
End
Where L.battingOrder Between 11 And 20
And L.active = 1
In fact, you could make it handle both home and away like so:
Select LNext.player As NextPlayer
From lineups As L
Left Join lineups As LNext
On LNext.BattingOrder = Case
When L.BattingOrder = 19 Then 11
When L.BattingOrder = 9 Then 1
Else L.BattingOrder + 1
End
Where L.active = 1