日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

LeetCode MySQL 1194. 锦标赛优胜者

發布時間:2024/7/5 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode MySQL 1194. 锦标赛优胜者 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

Players 玩家表

+-------------+-------+ | Column Name | Type | +-------------+-------+ | player_id | int | | group_id | int | +-------------+-------+ 玩家 ID 是此表的主鍵。 此表的每一行表示每個玩家的組。

Matches 賽事表

+---------------+---------+ | Column Name | Type | +---------------+---------+ | match_id | int | | first_player | int | | second_player | int | | first_score | int | | second_score | int | +---------------+---------+ match_id 是此表的主鍵。 每一行是一場比賽的記錄,第一名和第二名球員包含每場比賽的球員 ID。 第一個玩家和第二個玩家的分數分別包含第一個玩家和第二個玩家的分數。 你可以假設,在每一場比賽中,球員都屬于同一組。

每組的獲勝者是在組內得分最高的選手。
如果平局,player_id 最小 的選手獲勝。

編寫一個 SQL 查詢來查找每組中的獲勝者。

查詢結果格式如下所示

Players 表: +-----------+------------+ | player_id | group_id | +-----------+------------+ | 15 | 1 | | 25 | 1 | | 30 | 1 | | 45 | 1 | | 10 | 2 | | 35 | 2 | | 50 | 2 | | 20 | 3 | | 40 | 3 | +-----------+------------+Matches 表: +------------+--------------+---------------+-------------+--------------+ | match_id | first_player | second_player | first_score | second_score | +------------+--------------+---------------+-------------+--------------+ | 1 | 15 | 45 | 3 | 0 | | 2 | 30 | 25 | 1 | 2 | | 3 | 30 | 15 | 2 | 0 | | 4 | 40 | 20 | 5 | 2 | | 5 | 35 | 50 | 1 | 1 | +------------+--------------+---------------+-------------+--------------+Result 表: +-----------+------------+ | group_id | player_id | +-----------+------------+ | 1 | 15 | | 2 | 35 | | 3 | 40 | +-----------+------------+

來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/tournament-winners
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

2. 解題

  • 先把分數加起來
select player_id, sum(score) total_score from ((select first_player player_id, first_score scorefrom Matches)union all(select second_player, second_scorefrom Matches) ) t1 group by player_id {"headers": ["player_id", "total_score"], "values": [[15, 3], [30, 3], [40, 5], [35, 1], [45, 0], [25, 2], [20, 2], [50, 1]]}
  • 左連接獲取 分組id
  • 窗口函數獲取 組內排名
select group_id, player_id,rank() over(partition by group_id order by total_score desc, player_id) rnk from (select group_id, t2.player_id, total_scorefrom Players p left join(select player_id, sum(score) total_scorefrom((select first_player player_id, first_score scorefrom Matches)union all(select second_player, second_scorefrom Matches)) t1group by player_id) t2using(player_id) ) t3
  • 取出排名為1的
# Write your MySQL query statement belowselect group_id, player_id from (select group_id, player_id,rank() over(partition by group_id order by total_score desc, player_id) rnkfrom(select group_id, t2.player_id, total_scorefrom Players p left join(select player_id, sum(score) total_scorefrom((select first_player player_id, first_score scorefrom Matches)union all(select second_player, second_scorefrom Matches)) t1group by player_id) t2using(player_id)) t3 ) t where rnk = 1

我的CSDN博客地址 https://michael.blog.csdn.net/

長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!

總結

以上是生活随笔為你收集整理的LeetCode MySQL 1194. 锦标赛优胜者的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。