3246. 英超积分榜排名

力扣题目跳转 (3246. 英超积分榜排名 - 力扣(LeetCode)

表:TeamStats

+------------------+---------+
| Column Name      | Type    |
+------------------+---------+
| team_id          | int     |
| team_name        | varchar |
| matches_played   | int     |
| wins             | int     |
| draws            | int     |
| losses           | int     |
+------------------+---------+
team_id 是这张表的唯一主键。
这张表包含队伍 id,队伍名,场次,赢局,平局和输局。

 题目要求:

编写一个解决方案来计算联盟中每支球队的 得分 和 排名。积分计算方式如下:

  • 赢局 有 3 点得分
  • 平局 有 1 点得分
  • 输局 有 0 点得分

注意:积分相同的球队必须分配相同的排名。

返回结果表以 points 降序 排序,然后以 team_name 升序 排序。

结果格式如下所示。

示例:

输入:

TeamStats 表:

+---------+-----------------+----------------+------+-------+--------+
| team_id | team_name       | matches_played | wins | draws | losses |
+---------+-----------------+----------------+------+-------+--------+
| 1       | Manchester City | 10             | 6    | 2     | 2      |
| 2       | Liverpool       | 10             | 6    | 2     | 2      |
| 3       | Chelsea         | 10             | 5    | 3     | 2      |
| 4       | Arsenal         | 10             | 4    | 4     | 2      |
| 5       | Tottenham       | 10             | 3    | 5     | 2      |
+---------+-----------------+----------------+------+-------+--------+

输出:

+---------+-----------------+--------+----------+
| team_id | team_name       | points | position |
+---------+-----------------+--------+----------+
| 2       | Liverpool       | 20     | 1        |
| 1       | Manchester City | 20     | 1        |
| 3       | Chelsea         | 18     | 3        |
| 4       | Arsenal         | 16     | 4        |
| 5       | Tottenham       | 14     | 5        |
+---------+-----------------+--------+----------+

解释:

  • 曼城和利物浦均拿下 20 分(6 赢 * 3 分 + 2 平 * 1 分),所以他们并列第一。
  • 切尔西拿下 18 分(5 赢 * 3 分 + 3 平 * 1 分)所以位列第三。
  • 阿森纳拿下 16 分(4 赢 * 3 分 + 4 平 * 1 分)位列第四。
  • 托特纳姆热刺队拿下 14 分(3 赢 * 3 分 + 5 平 * 1 分)位列第五。

输出表以得分降序排序,然后以 team_name 升序排序。

case 1 的建表语句。

Create table if not exists TeamStats( team_id int, team_name varchar(100),matches_played int, wins int,draws int,losses int)

Truncate table TeamStats

insert into TeamStats (team_id, team_name, matches_played, wins, draws, losses) values ('1', 'Manchester City', '10', '6', '2', '2')

insert into TeamStats (team_id, team_name, matches_played, wins, draws, losses) values ('2', 'Liverpool', '10', '6', '2', '2')

insert into TeamStats (team_id, team_name, matches_played, wins, draws, losses) values ('3', 'Chelsea', '10', '5', '3', '2')

insert into TeamStats (team_id, team_name, matches_played, wins, draws, losses) values ('4', 'Arsenal', '10', '4', '4', '2')

insert into TeamStats (team_id, team_name, matches_played, wins, draws, losses) values ('5', 'Tottenham', '10', '3', '5', '2')

一     按照要求我们将相应的列拿出来 ,并且将分数求出来。

select
    team_id,
    team_name,
    (wins * 3 + draws * 1) as points
from TeamStats;

输出如下

二    使用 CTE 增加一列作为排名,要求是需要并列不连续的。我们可以使用 rank 函数。

with tmp as

(select

    team_id,

    team_name,

    (wins * 3 + draws * 1) as points

from TeamStats)

select *,

      rank() over(order by points desc) as position

from tmp

order by points desc,team_name;

最后按照要求对字段进行排序。

以上就是全部答案,如果对你有帮助请点个赞,谢谢。

来源:力扣(leecode

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 转载请注明出处:

3246. 英超积分榜排名-CSDN博客来源:力扣(leecode著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。转载请注明出处:我会尽快把力扣上的所有数据库题目发出来。每天不定时跟新。https://blog.csdn.net/CYJ1844/article/details/143068371

我会尽快把力扣上的所有数据库题目发出来。感兴趣的可以点个赞与关注。每天不定时跟新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值