I have a table
Team Matches Won
A 10 2
B 20 4
C 40 10
I want to convert this table into following
Team Won/Lost Won Lost Number
A Won 2
A Lost 8
B Won 4
B Lost 16
C Won 10
C Lost 30
Thanks in advance !
In fact, there is no aggregation here - just a simple subtraction. I'd do this in two queries, one for wins and one for loses, and combine them with the union all operator:
SELECT team, 'Won' AS "Won/Lost", won AS "Won/Lost Number"
FROM my_table
UNION ALL
SELECT team, 'Lost', matches - won
FROM my_table
ORDER BY 1 ASC, 2 DESC
Related
I have a table looking like this:
Team Name Points
A Peter 26
A John 18
A Carl 20
A Robert 32
A Mike 10
B Tom 22
B Michael 28
B Tina 18
B Donald 35
B Jeff 20
I want to get a result from the query that will give me the best 3 users from a team and the SUM of the point from the 3 highest users.
For team A the 3 highest scores are Robert (32), Peter (26) and (Carl (20) which is a total of 78 points.
For team B the highest 3 scores are Donald (35), Michael (28) and Tom (22) which is a total of 85 points
So it must be something like this:
Place Team Points
1 B 85
2 A 78
I have tried something like this:
SELECT points, user FROM table ORDER BY points DESC, LIMIT 3
That will give me the 3 users with the highest points but I want also the SUM of these 3 users and these records must be per team.
I think it must be done with a subquery, is this correct?
SELECT team,SUM(points) sp FROM
(
(
SELECT * FROM `table` WHERE team = "A" ORDER BY points DESC LIMIT 3
)
UNION ALL
(
SELECT * FROM `table` WHERE team = "B" ORDER BY points DESC LIMIT 3
)
)
AS dbx
GROUP BY team
ORDER BY sp DESC
Explaination:
Your Data:
Get Highest 3 from both Team (A and B).
And then, sum After join them All.
Result:
In MySQL, I am trying to sum values in a column given certain conditions. I have an example of some data below
Team Season Mth Stat
A 1 1 4
A 1 1 4
A 1 2 7
A 1 2 9
B 1 1 6
B 1 1 6
B 1 2 6
B 1 2 9
C 1 1 1
C 1 1 3
C 1 2 3
C 1 2 6
But I need the output to show up as
Team Season Mth Stat
A 1 1 8
A 1 2 16
B 1 1 12
B 1 2 15
C 1 1 4
C 1 2 9
So the Stat column is now the sum of the cells such that Match, Season, and Team are all the same. I have the code below. I see a lot of answers that use 'case' but that seems to be given logical operators that are not equal to each other. When I do it below, now it doesn't recognise the table where the columns are coming from. I do have a inner joins but the data itself is from one table. I get another error as well on the sum function because it requires one argument.
select
Team
,Season
,Match
--this is where I get lost-----------
sum(
select
Stat
From
table
Where
Mth=Mth
AND Season=Season
AND Team=Team
)
--end of getting lost----------------
FROM
table
Where
Season IN (1,2)
GROUP BY
Team
,Season
,Mth
Order BY
Team ASC
Edit:
It turns out I need to use GROUP BY as the comments suggest. So I am not summing within a table, but I sum the variable given the Group By parameters.
Unless I'm missing something, it's simply:
SELECT Team
,Season
,Match
,Sum(Stat)
FROM table
GROUP BY
Team
,Season
,Match
It's simple as this:
SELECT Team,
Season,
Match,
SUM(Stat)
FROM Table
WHERE Season IN (1,2)
GROUP BY Team,
Season,
Match
ORDER BY Team ASC
Please look at the SQL Fiddle example.
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 have a table with winner and loser statistics from a game:
id winner_id loser_id
1 1 2
2 1 2
3 3 4
4 4 3
5 1 2
6 2 1
7 3 4
8 3 2
9 3 5
10 3 6
11 2 3
12 3 6
13 2 3
I want a result table where i can find the highest winning streak of every player in the game. A streak of a player is broken, when he lost a game (player_id = loser_id). It should look like:
player_id win_streak
1 3
2 2
3 4
4 1
5 0
6 0
I tried many queries with user defined variables etc. but i can't find a solution. Thanks!
SQL Fiddle : http://sqlfiddle.com/#!9/3da5f/1
Is this the same as Alex's approach; I'm not quite sure, except that it seems to have one distinct advantage.... ;-)
SELECT player_id, MAX(CASE WHEN result = 'winner' THEN running ELSE 0 END) streak
FROM
( SELECT *
, IF(player_id = #prev_player,IF(result=#prev_result,#i:=#i+1,#i:=1),#i:=1) running
, #prev_result := result
, #prev_player:=player_id
FROM
( SELECT id, 'winner' result, winner_id player_id FROM my_table
UNION
SELECT id, 'loser', loser_id FROM my_table
) x
,
( SELECT #i:=1,#prev_result = '',#prev_player:='' ) vars
ORDER
BY x.player_id
, x.id
) a
GROUP
BY player_id;
I guess you should better to do that on php (or any other language you use) side.
But just to give you some idea and as experiment and example for some unique cases (hope it could be useful somewhere)
Here is my approach:
http://sqlfiddle.com/#!9/57cc65/1
SELECT r.winner_id,
(SELECT MAX(IF(winner_id=r.winner_id,IF(#i IS NULL, #i:=1,#i:=#i+1), IF(loser_id = r.winner_id, #i:=0,0)))
FROM Results r1
WHERE r1.winner_id = r.winner_id
OR r1.loser_id = r.winner_id
GROUP BY IF(winner_id=r.winner_id, winner_id,loser_id)) win_streak
FROM ( SELECT winner_id
FROM Results
GROUP BY winner_id
) r
It returns not all ids now but only who had ever win. So to make it better, probably you have user table. If so it would simplify a query. If you have no user table you need to union all somehow users who had never win.
You are welcome if any questions.
table league
team_id name wins losses played recorded created
1 dodgers 10 4 14 1364790000 1353215830
2 angels 9 6 15 1364790000 1353661376
3 pirates 12 3 15 1364790000 1353543466
team_id name wins losses played recorded created
1 dodgers 22 9 31 1367274480 1353215830
2 angels 14 17 31 1367274480 1353661376
3 pirates 19 13 32 1367274480 1353543466
4 yankees 10 9 19 1367274480 1365577298
5 brewers 7 11 18 1367274480 1365394448
Would like Results as:
team_id name wins losses played
1 dodgers 12 5 17
2 angels 5 11 16
3 pirates 7 10 17
4 yankees 10 9 19
5 brewers 7 11 18
I've tried several queries with joins and have had no success. Every day the team, wins, lossed and played are captured and time stamped on the recorded column. The team was created on the created column. (All unix timestamps) There are several rows in between the 2 dates I'm trying for, but I don't need them for this query.
What I wanted to do was to get April's Won/Loss/Played for existing and new teams, I tried several queries, here are a couple that did not give me the desired results:
SELECT a.name as name, a.wins-b.wins as wins, a.losses-b.losses as losses, a.played-b.played as played from league a join league b on a.id=b.id where a.recorded= 1367274480 and b.recorded= 1364790000
and
SELECT new.*, new.wins-old.wins as newwins, new.losses-old.losses as newlosses FROM league new LEFT JOIN league old ON new.id=old.id WHERE (new.recorded=1367274480 and old.recorded=1364790000) or (new.created > 1364790000 and new.recorded=1367274480) GROUP BY new.id
You want every row for the later records and the same number of rows for the earlier, so you need to use LEFT JOIN to get NULL's for teams created between two dates, but WHERE recorded condition for the smaller table should be moved as ON condition for the join.
Also keep in mind that 2-NULL = NULL, so you need to change NULL's into 0's with coalesce().
SELECT a.name AS name,
a.wins - COALESCE( b.wins, 0 ) AS wins,
a.losses - COALESCE( b.losses, 0 ) AS losses,
a.played - COALESCE( b.played, 0 ) AS played
FROM league a LEFT JOIN league b
ON a.team_id = b.team_id AND b.recorded =1364790000
WHERE a.recorded =1367274480
The way the data is set up, it seems that you don't have a recorded value on each day. However, each column would be increasing, so you can take the difference between the max and min vals for the month.
Try this:
SELECT l.name as name,
max(l.wins)-min(l.wins) as wins,
max(l.losses)-min(l.losses) as losses,
max(l.played)-min(l.played) as played
from league l
where l.recorded <= 1367274480 and l.recorded >= 1364790000
group by l.name