> For the complete documentation index, see [llms.txt](https://chiu-kuohsin.gitbook.io/leetcode-database-solution-with-hive-sql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chiu-kuohsin.gitbook.io/leetcode-database-solution-with-hive-sql/exercise-12-1194.tournament-winners/hive-solution.md).

# Hive Solution

## 3.Hive Solution

```sql
SELECT
x.group_id,x.player_id
FROM
(SELECT
t2.group_id,t1.player_id,score_ttl,
row_number()OVER(partition by t2.group_id order by t1.score_ttl desc,t1.player_id ) as rn
FROM
  (select 
   	player_id,
   	sum(score) as score_ttl
  from 
  	(select first_player as player_id,first_score as score from leetcode.ex_1194_matches
  	UNION ALL
  	select second_player as player_id, second_score as score from leetcode.ex_1194_matches ) a 
group by player_id
	) t1
 join (select player_id,group_id from ex_1194_players) t2 ON t1.player_id=t2.player_id
) x
WHERE x.rn=1 
;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chiu-kuohsin.gitbook.io/leetcode-database-solution-with-hive-sql/exercise-12-1194.tournament-winners/hive-solution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
