ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode:Database 20. League Statistics

2021-05-14 22:59:14  阅读:291  来源: 互联网

标签:League Statistics 20 goal away goals team home id


要求:Write an SQL query to report the statistics of the league. The statistics should be built using the played matches where the winning team gets three points and the losing team gets no points. If a match ends with a draw, both teams get one point.

Teams的结构:

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| team_id        | int     |
| team_name      | varchar |
+----------------+---------+
team_id is the primary key for this table.
Each row contains information about one team in the league.

Matches表的结构:

+-----------------+---------+
| Column Name     | Type    |
+-----------------+---------+
| home_team_id    | int     |
| away_team_id    | int     |
| home_team_goals | int     |
| away_team_goals | int     |
+-----------------+---------+
(home_team_id, away_team_id) is the primary key for this table.
Each row contains information about one match.
home_team_goals is the number of goals scored by the home team.
away_team_goals is the number of goals scored by the away team.
The winner of the match is the team with the higher number of goals.

Each row of the result table should contain:

team_name:The name of the team in the Teams table.
matches_played:The number of matches played as either a home or away team.
points:The total points the team has so far.
goal_for:The total number of goals scored by the team across all matches.
goal_against :The total number of goals scored by opponent teams against this team across all matches.
goal_diff:The result of goal_for - goal_against.
Return the result table in descending order by points. If two or more teams have the same points, order them in descending order by goal_diff. If there is still a tie, order them by team_name in lexicographical order.

The query result format is in the following example:

Teams table:

+---------+-----------+
| team_id | team_name |
+---------+-----------+
| 1       | Ajax      |
| 4       | Dortmund  |
| 6       | Arsenal   |
+---------+-----------+

Matches table:

+--------------+--------------+-----------------+-----------------+
| home_team_id | away_team_id | home_team_goals | away_team_goals |
+--------------+--------------+-----------------+-----------------+
| 1            | 4            | 0               | 1               |
| 1            | 6            | 3               | 3               |
| 4            | 1            | 5               | 2               |
| 6            | 1            | 0               | 0               |
+--------------+--------------+-----------------+-----------------+

Result table:

+-----------+----------------+--------+----------+--------------+-----------+
| team_name | matches_played | points | goal_for | goal_against | goal_diff |
+-----------+----------------+--------+----------+--------------+-----------+
| Dortmund  | 2              | 6      | 6        | 2            | 4         |
| Arsenal   | 2              | 2      | 3        | 3            | 0         |
| Ajax      | 4              | 2      | 5        | 9            | -4        |
+-----------+----------------+--------+----------+--------------+-----------+

Ajax (team_id=1) played 4 matches: 2 losses and 2 draws. Total points = 0 + 0 + 1 + 1 = 2.
Dortmund (team_id=4) played 2 matches: 2 wins. Total points = 3 + 3 = 6.
Arsenal (team_id=6) played 2 matches: 2 draws. Total points = 1 + 1 = 2.
Dortmund is the first team in the table. Ajax and Arsenal have the same points, but since Arsenal has a higher goal_diff than Ajax, Arsenal comes before Ajax in the table.

分析:
1.根据所求结果,要分三种情况判断,第一种情况仅作为主队,第二种情况仅作为客队,第三种情况作为主客队都进行了比赛
2.第一种情况和第二种情况,我们仅需要求出单张表中的数据即可得到最终会结果,第三种情况使用作为主队的数据+作为客队的数据是最终数据,三张情况合并即为最终结果

SQL语句:

SELECT team_name,matches_played,points,goal_for,goal_against,goal_diff FROM(
WITH a AS
(SELECT COUNT(home_team_id) AS m1,home_team_id AS i1,SUM(home_team_goals) AS g1,
SUM(away_team_goals) AS t1,
SUM(CASE  WHEN home_team_goals>away_team_goals THEN 3
WHEN home_team_goals=away_team_goals THEN 1
ELSE 0 END)AS p1
FROM matches
GROUP BY home_team_id
), b AS
(SELECT COUNT(away_team_id) AS m1,away_team_id AS i1,SUM(away_team_goals) AS g1,
SUM(home_team_goals)AS t1,
SUM(CASE  WHEN home_team_goals>away_team_goals THEN 0
WHEN home_team_goals=away_team_goals THEN 1
ELSE 3 END)AS p1
FROM matches
GROUP BY away_team_id)



SELECT	IF(c.team_name IS NULL,NULL,c.team_name) AS team_name,b.m1 AS matches_played,b.p1 AS points,
b.g1 AS goal_for,b.t1 AS goal_against,(b.g1-b.t1)AS goal_diff
FROM
b,teams c
WHERE
b.i1 NOT IN(SELECT b.i1 FROM
a JOIN  b
ON b.i1 IN(a.i1))
AND
c.team_id=b.i1

UNION ALL



SELECT	IF(c.team_name IS NULL,NULL,c.team_name) AS team_name,a.m1 AS matches_played,a.p1 AS points,
a.g1 AS goal_for,a.t1 AS goal_against,(a.g1-a.t1)AS goal_diff
FROM
a,teams c
WHERE c.team_id=a.i1  
AND a.i1 NOT IN(SELECT a.i1 FROM
a JOIN  b
ON a.i1 IN(b.i1))

UNION ALL

SELECT	c.team_name AS team_name,(a.m1+b.m1) AS matches_played,(a.p1+b.p1) AS points,
(a.g1+b.g1) AS goal_for,(a.t1+b.t1) AS goal_against,(a.g1+b.g1)-(a.t1+b.t1)AS goal_diff
FROM
a
JOIN
b
ON a.i1=b.i1 
JOIN teams c
ON c.team_id=a.i1 

)d
ORDER BY points DESC,goal_diff DESC,team_name;

标签:League,Statistics,20,goal,away,goals,team,home,id
来源: https://blog.csdn.net/qq_43713049/article/details/116808429

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有