LeetCode 1212. Team Scores in Football Tournament

Table: Teams

+---------------+----------+
| Column Name   | Type     |
+---------------+----------+
| team_id       | int      |
| team_name     | varchar  |
+---------------+----------+
team_id is the primary key of this table.
Each row of this table represents a single football team.

Table: Matches

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| match_id      | int     |
| host_team     | int     |
| guest_team    | int     |
| host_goals    | int     |
| guest_goals   | int     |
+---------------+---------+
match_id is the primary key of this table.
Each row is a record of a finished match between two different teams.
Teams host_team and guest_team are represented by their IDs in the teams table (team_id) and they scored host_goals and guest_goals goals respectively.

You would like to compute the scores of all teams after all matches. Points are awarded as follows:

  • A team receives three points if they win a match (Score strictly more goals than the opponent team).
  • A team receives one point if they draw a match (Same number of goals as the opponent team).
  • A team receives no points if they lose a match (Score less goals than the opponent team).

Write an SQL query that selects the team_id, team_name and num_points of each team in the tournament after all described matches. Result table should be ordered by num_points (decreasing order). In case of a tie, order the records by team_id (increasing order).

The query result format is in the following example:

Teams table:
+-----------+--------------+
| team_id   | team_name    |
+-----------+--------------+
| 10        | Leetcode FC  |
| 20        | NewYork FC   |
| 30        | Atlanta FC   |
| 40        | Chicago FC   |
| 50        | Toronto FC   |
+-----------+--------------+

Matches table:
+------------+--------------+---------------+-------------+--------------+
| match_id   | host_team    | guest_team    | host_goals  | guest_goals  |
+------------+--------------+---------------+-------------+--------------+
| 1          | 10           | 20            | 3           | 0            |
| 2          | 30           | 10            | 2           | 2            |
| 3          | 10           | 50            | 5           | 1            |
| 4          | 20           | 30            | 1           | 0            |
| 5          | 50           | 30            | 1           | 0            |
+------------+--------------+---------------+-------------+--------------+

Result table:
+------------+--------------+---------------+
| team_id    | team_name    | num_points    |
+------------+--------------+---------------+
| 10         | Leetcode FC  | 7             |
| 20         | NewYork FC   | 3             |
| 50         | Toronto FC   | 3             |
| 30         | Atlanta FC   | 1             |
| 40         | Chicago FC   | 0             |
+------------+--------------+---------------+

简要翻译:

两张表:队伍表和比赛表。统计队伍积分,胜3分,平1分,负0分,查询个队伍排名榜,查询结果先按积分降序,然后按team_id升序排列。

建表语句:

CREATE TABLE Teams (
  team_id INT ,
  team_name VARCHAR (20)
) ;

CREATE TABLE Matches (
  match_id INT ,
  host_team INT ,
  guest_team INT ,
  host_goals  INT,
  guest_goals INT
) ;

INSERT INTO Teams (team_id, team_name)
VALUES
  (10, ‘Leetcode FC‘) ;

INSERT INTO Teams (team_id, team_name)
VALUES
  (20, ‘NewYork FC‘) ;

INSERT INTO Teams (team_id, team_name)
VALUES
  (30, ‘Atlanta FC‘) ;

INSERT INTO Teams (team_id, team_name)
VALUES
  (40, ‘Chicago FC‘) ;

INSERT INTO Teams (team_id, team_name)
VALUES
  (50, ‘Toronto FC‘) ;

INSERT INTO Matches (
  match_id,
  host_team,
  guest_team,
  host_goals,
  guest_goals
)
VALUES
  (1, 10, 20, 3, 0) ;

INSERT INTO Matches (
  match_id,
  host_team,
  guest_team,
  host_goals,
  guest_goals
)
VALUES
  (2, 30, 10, 2, 2) ;

INSERT INTO Matches (
  match_id,
  host_team,
  guest_team,
  host_goals,
  guest_goals
)
VALUES
  (3, 10, 50, 5, 1) ;

INSERT INTO Matches (
  match_id,
  host_team,
  guest_team,
  host_goals,
  guest_goals
)
VALUES
  (4, 20, 30, 1, 0) ;

INSERT INTO Matches (
  match_id,
  host_team,
  guest_team,
  host_goals,
  guest_goals
)
VALUES
  (5, 50, 30, 1, 0) ;

SQL思路:

一、查询每场比赛各队伍得分:

 # 作为主队的得分统计: 1 SELECT
 2   team_id,
 3   team_name,
 4   IF(
 5     host_goals > guest_goals,
 6     3,
 7     IF(host_goals = guest_goals, 1, 0)
 8   ) AS points
 9 FROM
10   teams,
11   matches
12 WHERE team_id = host_team;
 #作为客队的得分统计 1 SELECT
 2   team_id,
 3   team_name,
 4   IF(
 5     host_goals > guest_goals,
 6     0,
 7     IF(host_goals = guest_goals, 1, 3)
 8   ) AS points
 9 FROM
10   teams,
11   matches
12 WHERE team_id = guest_team;

二、将上两张表union all ,然后分组聚合查询,然后发现少了没参赛队伍的积分。因此需要left join 。最终查询语句如下

 1 SELECT
 2   t.team_id,
 3   t.team_name,
 4   SUM(IFNULL(points,0)) AS num_points
 5 FROM
 6 teams t LEFT JOIN
 7 (
 8 SELECT
 9   team_id,
10   team_name,
11   IF(
12     host_goals > guest_goals,
13     3,
14     IF(host_goals = guest_goals, 1, 0)
15   ) AS points
16 FROM
17   teams,
18   matches
19 WHERE team_id = host_team
20 UNION ALL
21 SELECT
22   team_id,
23   team_name,
24   IF(
25     host_goals > guest_goals,
26     0,
27     IF(host_goals = guest_goals, 1, 3)
28   ) AS points
29 FROM
30   teams,
31   matches
32 WHERE team_id = guest_team
33 )temp ON
34 t.team_id = temp.team_id
35 GROUP BY team_id
36 ORDER BY num_points DESC ,team_id ASC ;

原文地址:https://www.cnblogs.com/onePunchCoder/p/11619433.html

时间: 2024-10-13 22:06:41

LeetCode 1212. Team Scores in Football Tournament的相关文章

LeetCode:Rank Scores - 按分数排名次

1.题目名称 Rank Scores(按分数排名次) 2.题目地址 https://leetcode.com/problems/rank-scores/ 3.题目内容 按分数排名次,如果两个Id的分数一样,那么他们的名次是一样的,排名从1开始.注意,每组分数的名次,都是上一组分数名次加一. 例如,有这样一组数据: +----+-------+ | Id | Score | +----+-------+ | 1  | 3.50  | | 2  | 3.65  | | 3  | 4.00  | | 

[LeetCode][SQL]Rank Scores

Rank Scores Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "ho

Leetcode 178. Rank Scores (Database)

参考: https://my.oschina.net/Tsybius2014/blog/493244 常见的排序1. 排名1,2,3,...,n 不对重复的分数做特殊处理 Score  Rank 100     1 99     2 99     3 98     4 90     5 # Write your MySQL query statement below select Score, @Counter := @Counter + 1 as rank from Scores, (sele

LeetCode Database: Rank Scores

Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" be

sql -leetcode 178. Rank Scores

Score 很好得到: select Score from Scores order by Score desc; 要得到rank, 可以通过比较比当前Score 大的Score 的个数得到: select Score, (select count(distinct Score) from Scores where Score>=s.Score) Rank where Scores s order by Score desc; 或者: select s.Score ,count(distinct

POJ 3071 Football

Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the tournament are placed in a list in order of increasing index. Then, the first team in the l

CodeForces 432B Football Kit(水)

Description Consider a football tournament where n teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the i-th team has color xi and the kit for away games of this team has color yi(xi ≠ 

Football

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2882   Accepted: 1466 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2n. In each round of the tournament, all teams still in the tourname

POJ 3071 Football (动态规划-概率DP)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2768   Accepted: 1412 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the