返回介绍

solution / 2100-2199 / 2175.The Change in Global Rankings / README_EN

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

2175. The Change in Global Rankings

中文文档

Description

Table: TeamPoints

+-------------+---------+
| Column Name | Type  |
+-------------+---------+
| team_id   | int   |
| name    | varchar |
| points    | int   |
+-------------+---------+
team_id contains unique values.
Each row of this table contains the ID of a national team, the name of the country it represents, and the points it has in the global rankings. No two teams will represent the same country.

 

Table: PointsChange

+---------------+------+
| Column Name   | Type |
+---------------+------+
| team_id     | int  |
| points_change | int  |
+---------------+------+
team_id contains unique values.
Each row of this table contains the ID of a national team and the change in its points in the global rankings.
points_change can be:
- 0: indicates no change in points.
- positive: indicates an increase in points.
- negative: indicates a decrease in points.
Each team_id that appears in TeamPoints will also appear in this table.

 

The global ranking of a national team is its rank after sorting all the teams by their points in descending order. If two teams have the same points, we break the tie by sorting them by their name in lexicographical order.

The points of each national team should be updated based on its corresponding points_change value.

Write a solution to calculate the change in the global rankings after updating each team's points.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
TeamPoints table:
+---------+-------------+--------+
| team_id | name    | points |
+---------+-------------+--------+
| 3     | Algeria   | 1431   |
| 1     | Senegal   | 2132   |
| 2     | New Zealand | 1402   |
| 4     | Croatia   | 1817   |
+---------+-------------+--------+
PointsChange table:
+---------+---------------+
| team_id | points_change |
+---------+---------------+
| 3     | 399       |
| 2     | 0       |
| 4     | 13      |
| 1     | -22       |
+---------+---------------+
Output: 
+---------+-------------+-----------+
| team_id | name    | rank_diff |
+---------+-------------+-----------+
| 1     | Senegal   | 0     |
| 4     | Croatia   | -1    |
| 3     | Algeria   | 1     |
| 2     | New Zealand | 0     |
+---------+-------------+-----------+
Explanation: 
The global rankings were as follows:
+---------+-------------+--------+------+
| team_id | name    | points | rank |
+---------+-------------+--------+------+
| 1     | Senegal   | 2132   | 1  |
| 4     | Croatia   | 1817   | 2  |
| 3     | Algeria   | 1431   | 3  |
| 2     | New Zealand | 1402   | 4  |
+---------+-------------+--------+------+
After updating the points of each team, the rankings became the following:
+---------+-------------+--------+------+
| team_id | name    | points | rank |
+---------+-------------+--------+------+
| 1     | Senegal   | 2110   | 1  |
| 3     | Algeria   | 1830   | 2  |
| 4     | Croatia   | 1830   | 3  |
| 2     | New Zealand | 1402   | 4  |
+---------+-------------+--------+------+
Since after updating the points Algeria and Croatia have the same points, they are ranked according to their lexicographic order.
Senegal lost 22 points but their rank did not change.
Croatia gained 13 points but their rank decreased by one.
Algeria gained 399 points and their rank increased by one.
New Zealand did not gain or lose points and their rank did not change.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  P AS (
    SELECT team_id, SUM(points_change) AS delta
    FROM PointsChange
    GROUP BY team_id
  )
SELECT
  team_id,
  name,
  CAST(RANK() OVER (ORDER BY points DESC, name) AS SIGNED) - CAST(
    RANK() OVER (ORDER BY (points + delta) DESC, name) AS SIGNED
  ) AS 'rank_diff'
FROM
  TeamPoints
  JOIN P USING (team_id);

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

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

发布评论

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