SQL to get Team Records from Games Database - mysql

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

Related

Unique & Rept Customer Sales based on date

I am new to Mysql, I have the data like this,
Order_dt User_ID Sales
28-03-2022 PRPK-12 84
28-03-2022 PRPK-11 41
28-03-2022 PRPK-10 55
28-03-2022 PRPK-12 76
26-03-2022 PRPK-10 54
27-03-2022 PRPK-11 88
27-03-2022 PRPK-11 51
27-03-2022 PRPK-10 40
27-03-2022 PRPK-10 40
& I need o/p like below the format.
Order_date Unique_Cx Unique_Cx_Sales Rept_Cx Rept_Cx_Sales
26-03-2022 1 54 0 0
27-03-2022 0 0 2 219
28-03-2022 2 96 1 160
I'm getting only unique & Rept count by using the 'Having' function, but im unable to map with the sales. Kindly help.
Thanking you.
First group by dt and user id in sub query then use conditional aggregation
select order_dt,
sum(case when cd = 1 then 1 else 0 end) unique_cx,
sum(case when cd = 1 then sales else 0 end) unique_cx_sales,
sum(case when cd > 1 then 1 else 0 end) repeat_cx,
sum(case when cd > 1 then sales else 0 end) repeat_cx_sales
from
(
select order_dt, user_id,
count(*) cd,sum(sales) sales
from t
group by order_dt,user_id
) s
group by order_dt
;

How to select win/loss record in mysql

I have table "games" that looks like this
| team1_id | team2_id | team1_pts | team2_pts |
1 3 101 117
2 5 99 98
I would like to do select that gives back team name from table "team" and win/loss record like this
| team_name | win | loss
Boston 15 11
Miami 13 12
I figured out how to get table with number of wins only but have no idea how to get wins and losses in same table.
You can unpivot and re-join:
select t.name, sum(is_win) as num_wins, sum(is_loss) as num_losses
from ((select (case when team1_pts > team2_pts then team1_id else team2_id end) as team_id,
1 as is_win, 0 as is_loss
from game g
) union all
(select (case when team1_pts < team2_pts then team1_id else team2_id end) as team_id,
0, 1
from game g
)
) g join
teams t
on t.team_id = g.team_id
group by t.name
I think you should revisit your data model. Maybe with GAMES table having fields GAME_ID, TEAM_ID, POINTS. You can probably get it working with what you have so far, but its not ideal.
This will do it:
select team_id ,
sum(case when Points > 0 then 1 else 0 end) as Win,
sum(case when Points < 0 then 1 else 0 end) as Loss
from
(
select team1_id as team_id , team1_pts - team2_pts as Points from games
union all
select team2_id, team2_pts - team1_pts as Points from games
)tbl
group by team_id

How to build this query?

i have table
http://oi58.tinypic.com/2s7xreo.jpg
id action_date type item_id quantity
--- ----------- ---- ------- --------
87 4/25/2014 1 s-1 100
88 4/1/2014 1 s-1 150
89 4/4/2014 1 s-1 200
90 4/3/2014 1 s-2 222
91 4/7/2014 1 s-2 10
96 4/4/2014 1 s-2 8
97 4/22/2014 1 s-2 8
98 4/21/2014 2 s-1 255
99 4/5/2014 2 s-1 6
100 4/6/2014 2 s-2 190
101 4/6/2014 2 s-3 96
102 4/8/2014 2 s-1 120
103 4/15/2014 2 s-2 3
104 4/16/2014 2 s-2 3
type column which mean if 1 this is in item to my shop >>> if 2 this is out item from my shop >>
i need query to give me result like this
item in out net
s1 300 195 105
and so on >>
how to write query that give me this result >>
and if i must but them in tow table >> table for the in and table for the out if that help >>> how it build the query >
and thanx in advance :)
note :: i am work on access
One way to get the result is to use an aggregate function on an expression that conditionally returns the value of quantity, based on the value of type.
SELECT t.item
, SUM(CASE WHEN t.type = 1 THEN t.quantity ELSE 0 END) AS in_
, SUM(CASE WHEN t.type = 2 THEN t.quantity ELSE 0 END) AS out_
, SUM(CASE WHEN t.type = 1 THEN t.quantity ELSE 0 END)
- SUM(CASE WHEN t.type = 2 THEN t.quantity ELSE 0 END) AS net_
FROM mytable t
GROUP BY t.item
This approach works in Oracle, as well as MySQL and SQL Server.
(If you remove the SUM() aggregate function and the GROUP BY clause, you can see how that CASE expression is working. The query above gives the result you specified, this one is just a demonstration that helps "explain" how that query works.)
SELECT t.item
, CASE WHEN t.type = 1 THEN t.quantity ELSE 0 END AS in_
, CASE WHEN t.type = 2 THEN t.quantity ELSE 0 END AS out_
, t.*
FROM mytable t
UPDATE
Unfortunately, Microsoft Access doesn't support CASE expressions. But Access does have an iif function. The same approach should work, the syntax might be something like this:
SELECT t.item
, SUM(iif(t.type = 1, t.quantity, 0) AS in_
, SUM(iif(t.type = 2, t.quantity, 0) AS out_
, SUM(iif(t.type = 1, t.quantity, 0)
- SUM(iif(t.type = 2, t.quantity, 0) AS net_
FROM mytable t
GROUP BY t.item
SELECT item_id,
In_col,
Out_col,
(In_col - Out_col) AS Net
FROM
(SELECT item_id,
sum(CASE WHEN TYPE = 1 THEN quantity END) AS In_col,
sum(CASE WHEN TYPE = 2 THEN quantity END) AS Out_col
FROM TABLE
GROUP BY item_id) AS t1;
I hope this what you need.

SQL for Game Scores & Prediction System

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

SQL - select highest win / loss

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