LeetCode:Database 20. League Statistics

要求: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;
上一篇:【题解】 CF1495E Qingshan and Daniel 官方中文题解


下一篇:Google Play上出现了针对移动设备的恶意软件