SELECTx.group_id,x.player_idFROM(SELECTt2.group_id,t1.player_id,score_ttl,row_number()OVER(partitionby t2.group_id order by t1.score_ttl desc,t1.player_id ) as rnFROM (select player_id,sum(score) as score_ttlfrom (select first_player as player_id,first_score as score from leetcode.ex_1194_matchesUNION ALLselect second_player as player_id, second_score as score from leetcode.ex_1194_matches ) a group by player_id ) t1join (select player_id,group_id from ex_1194_players) t2 ON t1.player_id=t2.player_id) xWHERE x.rn=1;