Skip to content

Dev sql #1100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2023
Merged

Dev sql #1100

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion solution/1100-1199/1194.Tournament Winners/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,37 @@ Players 表</code>:
### **SQL**

```sql

# Write your MySQL query statement below
WITH
s AS (
SELECT first_player AS player_id, first_score AS score, group_id
FROM
Matches AS m
JOIN Players AS p ON m.first_player = p.player_id
UNION ALL
SELECT second_player AS player_id, second_score AS score, group_id
FROM
Matches AS m
JOIN Players AS p ON m.second_player = p.player_id
),
t AS (
SELECT group_id, player_id, sum(score) AS scores
FROM s
GROUP BY player_id
),
p AS (
SELECT
group_id,
player_id,
rank() OVER (
PARTITION BY group_id
ORDER BY scores DESC, player_id
) AS rk
FROM t
)
SELECT group_id, player_id
FROM p
WHERE rk = 1;
```

<!-- tabs:end -->
32 changes: 31 additions & 1 deletion solution/1100-1199/1194.Tournament Winners/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,37 @@ Matches table:
### **SQL**

```sql

# Write your MySQL query statement below
WITH
s AS (
SELECT first_player AS player_id, first_score AS score, group_id
FROM
Matches AS m
JOIN Players AS p ON m.first_player = p.player_id
UNION ALL
SELECT second_player AS player_id, second_score AS score, group_id
FROM
Matches AS m
JOIN Players AS p ON m.second_player = p.player_id
),
t AS (
SELECT group_id, player_id, sum(score) AS scores
FROM s
GROUP BY player_id
),
p AS (
SELECT
group_id,
player_id,
rank() OVER (
PARTITION BY group_id
ORDER BY scores DESC, player_id
) AS rk
FROM t
)
SELECT group_id, player_id
FROM p
WHERE rk = 1;
```

<!-- tabs:end -->
31 changes: 31 additions & 0 deletions solution/1100-1199/1194.Tournament Winners/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Write your MySQL query statement below
WITH
s AS (
SELECT first_player AS player_id, first_score AS score, group_id
FROM
Matches AS m
JOIN Players AS p ON m.first_player = p.player_id
UNION ALL
SELECT second_player AS player_id, second_score AS score, group_id
FROM
Matches AS m
JOIN Players AS p ON m.second_player = p.player_id
),
t AS (
SELECT group_id, player_id, sum(score) AS scores
FROM s
GROUP BY player_id
),
p AS (
SELECT
group_id,
player_id,
rank() OVER (
PARTITION BY group_id
ORDER BY scores DESC, player_id
) AS rk
FROM t
)
SELECT group_id, player_id
FROM p
WHERE rk = 1;
38 changes: 37 additions & 1 deletion solution/1800-1899/1841.League Statistics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,43 @@ Dortmund 是积分榜上的第一支球队. Ajax和Arsenal 有同样的分数,
<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql

# Write your MySQL query statement below
WITH
Scores AS (
SELECT
home_team_id AS team_id,
CASE
WHEN home_team_goals > away_team_goals THEN 3
WHEN home_team_goals < away_team_goals THEN 0
ELSE 1
END AS score,
home_team_goals AS goals,
away_team_goals AS away_goals
FROM Matches
UNION ALL
SELECT
away_team_id AS team_id,
CASE
WHEN home_team_goals > away_team_goals THEN 0
WHEN home_team_goals < away_team_goals THEN 3
ELSE 1
END AS score,
away_team_goals AS goals,
home_team_goals AS away_goals
FROM Matches
)
SELECT
team_name,
count(1) AS matches_played,
sum(score) AS points,
sum(goals) AS goal_for,
sum(away_goals) AS goal_against,
(sum(goals) - sum(away_goals)) AS goal_diff
FROM
Scores AS s
JOIN Teams AS t ON s.team_id = t.team_id
GROUP BY s.team_id
ORDER BY points DESC, goal_diff DESC, team_name;
```

<!-- tabs:end -->
38 changes: 37 additions & 1 deletion solution/1800-1899/1841.League Statistics/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,43 @@ Dortmund is the first team in the table. Ajax and Arsenal have the same points,
### **SQL**

```sql

# Write your MySQL query statement below
WITH
Scores AS (
SELECT
home_team_id AS team_id,
CASE
WHEN home_team_goals > away_team_goals THEN 3
WHEN home_team_goals < away_team_goals THEN 0
ELSE 1
END AS score,
home_team_goals AS goals,
away_team_goals AS away_goals
FROM Matches
UNION ALL
SELECT
away_team_id AS team_id,
CASE
WHEN home_team_goals > away_team_goals THEN 0
WHEN home_team_goals < away_team_goals THEN 3
ELSE 1
END AS score,
away_team_goals AS goals,
home_team_goals AS away_goals
FROM Matches
)
SELECT
team_name,
count(1) AS matches_played,
sum(score) AS points,
sum(goals) AS goal_for,
sum(away_goals) AS goal_against,
(sum(goals) - sum(away_goals)) AS goal_diff
FROM
Scores AS s
JOIN Teams AS t ON s.team_id = t.team_id
GROUP BY s.team_id
ORDER BY points DESC, goal_diff DESC, team_name;
```

<!-- tabs:end -->
37 changes: 37 additions & 0 deletions solution/1800-1899/1841.League Statistics/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Write your MySQL query statement below
WITH
Scores AS (
SELECT
home_team_id AS team_id,
CASE
WHEN home_team_goals > away_team_goals THEN 3
WHEN home_team_goals < away_team_goals THEN 0
ELSE 1
END AS score,
home_team_goals AS goals,
away_team_goals AS away_goals
FROM Matches
UNION ALL
SELECT
away_team_id AS team_id,
CASE
WHEN home_team_goals > away_team_goals THEN 0
WHEN home_team_goals < away_team_goals THEN 3
ELSE 1
END AS score,
away_team_goals AS goals,
home_team_goals AS away_goals
FROM Matches
)
SELECT
team_name,
count(1) AS matches_played,
sum(score) AS points,
sum(goals) AS goal_for,
sum(away_goals) AS goal_against,
(sum(goals) - sum(away_goals)) AS goal_diff
FROM
Scores AS s
JOIN Teams AS t ON s.team_id = t.team_id
GROUP BY s.team_id
ORDER BY points DESC, goal_diff DESC, team_name;