返回介绍

solution / 1800-1899 / 1841.League Statistics / README

发布于 2024-06-17 01:03:14 字数 4284 浏览 0 评论 0 收藏 0

1841. 联赛信息统计

English Version

题目描述

表: Teams

+----------------+---------+
| Column Name  | Type  |
+----------------+---------+
| team_id    | int   |
| team_name    | varchar |
+----------------+---------+
team_id 是该表主键.
每一行都包含了一个参加联赛的队伍信息.

 

表: 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) 是该表主键.
每一行包含了一次比赛信息.
home_team_goals 代表主场队得球数.
away_team_goals 代表客场队得球数.
获得球数较多的队伍为胜者队伍.

 

写一段SQL,用来报告联赛信息. 统计数据应使用已进行的比赛来构建,其中 获胜 球队获得 三分 ,而失败球队获得 零分 。如果 打平 ,两支球队都得 一分 

result 表的每行应包含以下信息:

  • team_name - Teams 表中的队伍名字
  • matches_played - 主场与客场球队进行的比赛次数.
  • points - 球队获得的总分数.
  • goal_for - 球队在所有比赛中获取的总进球数
  • goal_against - 球队在所有比赛中,他的对手球队的所有进球数
  • goal_diff - goal_for - goal_against.

points 降序 返回结果表。 如果两队或多队得分相同,则按 goal_diff 降序 排列。 如果仍然存在平局,则以 team_name 按字典顺序 排列它们。

查询的结果格式如下例所示。

 

示例 1:

输入:
Teams 表:
+---------+-----------+
| team_id | team_name |
+---------+-----------+
| 1     | Ajax    |
| 4     | Dortmund  |
| 6     | Arsenal   |
+---------+-----------+
Matches 表:
+--------------+--------------+-----------------+-----------------+
| 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         |
+--------------+--------------+-----------------+-----------------+
输出:
+-----------+----------------+--------+----------+--------------+-----------+
| 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) 有4场比赛: 2败2平. 总分数 = 0 + 0 + 1 + 1 = 2.
Dortmund (team_id=4) 有2场比赛: 2胜. 总分数 = 3 + 3 = 6.
Arsenal (team_id=6) 有2场比赛: 2平. 总分数 = 1 + 1 = 2.
Dortmund 是积分榜上的第一支球队. Ajax和Arsenal 有同样的分数, 但Arsenal的goal_diff高于Ajax, 所以Arsenal在表中的顺序在Ajaxzhi'qian.

解法

方法一

# Write your MySQL query statement below
WITH
  Scores AS (
    SELECT
      home_team_id AS team_id,
      CASE
        WHEN home_team_goals > away_team_goals THEN 3
        WHEN home_team_goals < away_team_goals THEN 0
        ELSE 1
      END AS score,
      home_team_goals AS goals,
      away_team_goals AS away_goals
    FROM Matches
    UNION ALL
    SELECT
      away_team_id AS team_id,
      CASE
        WHEN home_team_goals > away_team_goals THEN 0
        WHEN home_team_goals < away_team_goals THEN 3
        ELSE 1
      END AS score,
      away_team_goals AS goals,
      home_team_goals AS away_goals
    FROM Matches
  )
SELECT
  team_name,
  COUNT(1) AS matches_played,
  SUM(score) AS points,
  SUM(goals) AS goal_for,
  SUM(away_goals) AS goal_against,
  (SUM(goals) - SUM(away_goals)) AS goal_diff
FROM
  Scores AS s
  JOIN Teams AS t ON s.team_id = t.team_id
GROUP BY s.team_id
ORDER BY points DESC, goal_diff DESC, team_name;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文