当前位置:网站首页>SQL: Championship winner
SQL: Championship winner
2022-06-09 08:32:00 【Jieyou grocery store Q】
Write a SQL Query to find the winners in each group .
The winner of each group is the player with the highest cumulative score in the group . If there is a draw ,player_id The youngest player wins .
Each line is the record of a game ,
first_player and second_player Indicates the player of the game id.
first_score and second_score respectively first_player and second_player Score of .
You can assume , In every game , The players belong to the same group .
CREATE TABLE players ( player_id INT, group_id INT );
CREATE TABLE matches (
match_id INT,
first_player INT,
second_player INT,
first_score INT,
second_score INT
);
REPLACE INTO players ( player_id, group_id ) VALUES
( 10, 2 ),( 15, 1 ),( 20, 3 ),( 25, 1 ),( 30, 1 ),( 35, 2 ),( 40, 3 ),( 45, 1 ),( 50, 2 );
REPLACE INTO matches ( match_id, first_player, second_player, first_score, second_score ) VALUES
( 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 );


Method 1
Every game , Each player has accumulated scores , Then the highest score is the highest score of the group
SELECT
group_id,
player_id
from
(
SELECT
group_id,
player_id,
rank() over ( PARTITION BY group_id ORDER BY sum( IF ( player_id = first_player, first_score, second_score ) ) DESC, player_id ) rn
FROM
matches a
LEFT JOIN players b ON a.first_player = b.player_id
OR a.second_player = b.player_id
GROUP BY
group_id,
player_id
) t
WHERE rn =1

边栏推荐
- 【TeXstudio】【3】较为完整的论文排版模板与bib文件引用方法
- [reading papers] efficientnet: retailing model scaling for revolutionary neural networks
- Market Research - current situation and future development trend of zirconia grinding wheel Market in the world and China
- 2022-2028 global Supplementary Cementitious Materials (SCM) industry research and trend analysis report
- leetcodeSQL:1070. Product sales analysis III
- JVM architecture learning notes
- 修改pe程序入口点
- Market Research - current market situation and future development trend of functional paper additives in the world and China
- GC study notes
- MySQL adds a string after the field
猜你喜欢

RMAN备份概念_关于RMAN增量备份(RMAN INCREMENTAL BACKUP)
![[pat (basic level) practice] - [sort] 1077 mutual evaluation score calculation](/img/0c/f9deb9b02936e1fc2a53ef3e44c6bf.jpg)
[pat (basic level) practice] - [sort] 1077 mutual evaluation score calculation

Blow up the idea artifact in use recently

RMAN备份概念_关于备份集(Backup Set)

Self made compiler learning 1: use of CB compiler

修改pe程序入口点

办公软件系列 之excel应用4

SQL:体育馆的人流量(连续日期案例通用解决方案)
![[reading point paper] ghostnet: more features from cheapoperations convolution operation is still expensive, and feature graph redundancy can be obtained by linear transformation](/img/9c/24efb72fadf0b3b69f2bb3908e6521.png)
[reading point paper] ghostnet: more features from cheapoperations convolution operation is still expensive, and feature graph redundancy can be obtained by linear transformation

Do you really know tcp/ip
随机推荐
MySQL adds a string after the field
84.1% of the parents surveyed felt that there were more parents around who liked to coax their children with electronic products
About Eigendecomposition
【读点论文】MobileDets: Searching for Object Detection Architectures for Mobile Accelerators,适配不同硬件平台的搜索方案
配置RMAN备份的环境_配置归档redo日志删除策略(DELETION POLICY)
About Eigendecomposition
SQL: 超过经理收入的员工
Leetcode: find the number of recent palindromes
2022-2028 global TSCM equipment industry research and trend analysis report
SQL:市场分析 I
JVM体系架构学习笔记
Implementation of WTM based on NETCORE framework
阿裏雲ack免密拉取企業版acr鏡像
Could not find artifact com. retail. stock:retail-stock-center:pom:1.0-SNAPSHOT in snapshots
mysql中if和ifnull
Self made compiler learning 1: use of CB compiler
[paper introduction] clip: image and natural language pairing pre training transferable visual models from natural language supervision
领域驱动设计(Domain Driven Design,DDD)
Question about Oracle: why can't the DBMS be linked according to the evening tutorial and output results
集成电路设计开源EDA软件yosys详解1:工具安装