|
| 1 | +-- Question 53 |
| 2 | +-- Table: Teams |
| 3 | + |
| 4 | +-- +---------------+----------+ |
| 5 | +-- | Column Name | Type | |
| 6 | +-- +---------------+----------+ |
| 7 | +-- | team_id | int | |
| 8 | +-- | team_name | varchar | |
| 9 | +-- +---------------+----------+ |
| 10 | +-- team_id is the primary key of this table. |
| 11 | +-- Each row of this table represents a single football team. |
| 12 | +-- Table: Matches |
| 13 | + |
| 14 | +-- +---------------+---------+ |
| 15 | +-- | Column Name | Type | |
| 16 | +-- +---------------+---------+ |
| 17 | +-- | match_id | int | |
| 18 | +-- | host_team | int | |
| 19 | +-- | guest_team | int | |
| 20 | +-- | host_goals | int | |
| 21 | +-- | guest_goals | int | |
| 22 | +-- +---------------+---------+ |
| 23 | +-- match_id is the primary key of this table. |
| 24 | +-- Each row is a record of a finished match between two different teams. |
| 25 | +-- Teams host_team and guest_team are represented by their IDs in the teams table (team_id) and they scored host_goals and guest_goals goals respectively. |
| 26 | + |
| 27 | + |
| 28 | +-- You would like to compute the scores of all teams after all matches. Points are awarded as follows: |
| 29 | +-- A team receives three points if they win a match (Score strictly more goals than the opponent team). |
| 30 | +-- A team receives one point if they draw a match (Same number of goals as the opponent team). |
| 31 | +-- A team receives no points if they lose a match (Score less goals than the opponent team). |
| 32 | +-- Write an SQL query that selects the team_id, team_name and num_points of each team in the tournament after all described matches. Result table should be ordered by num_points (decreasing order). In case of a tie, order the records by team_id (increasing order). |
| 33 | + |
| 34 | +-- The query result format is in the following example: |
| 35 | + |
| 36 | +-- Teams table: |
| 37 | +-- +-----------+--------------+ |
| 38 | +-- | team_id | team_name | |
| 39 | +-- +-----------+--------------+ |
| 40 | +-- | 10 | Leetcode FC | |
| 41 | +-- | 20 | NewYork FC | |
| 42 | +-- | 30 | Atlanta FC | |
| 43 | +-- | 40 | Chicago FC | |
| 44 | +-- | 50 | Toronto FC | |
| 45 | +-- +-----------+--------------+ |
| 46 | + |
| 47 | +-- Matches table: |
| 48 | +-- +------------+--------------+---------------+-------------+--------------+ |
| 49 | +-- | match_id | host_team | guest_team | host_goals | guest_goals | |
| 50 | +-- +------------+--------------+---------------+-------------+--------------+ |
| 51 | +-- | 1 | 10 | 20 | 3 | 0 | |
| 52 | +-- | 2 | 30 | 10 | 2 | 2 | |
| 53 | +-- | 3 | 10 | 50 | 5 | 1 | |
| 54 | +-- | 4 | 20 | 30 | 1 | 0 | |
| 55 | +-- | 5 | 50 | 30 | 1 | 0 | |
| 56 | +-- +------------+--------------+---------------+-------------+--------------+ |
| 57 | + |
| 58 | +-- Result table: |
| 59 | +-- +------------+--------------+---------------+ |
| 60 | +-- | team_id | team_name | num_points | |
| 61 | +-- +------------+--------------+---------------+ |
| 62 | +-- | 10 | Leetcode FC | 7 | |
| 63 | +-- | 20 | NewYork FC | 3 | |
| 64 | +-- | 50 | Toronto FC | 3 | |
| 65 | +-- | 30 | Atlanta FC | 1 | |
| 66 | +-- | 40 | Chicago FC | 0 | |
| 67 | +-- +------------+--------------+---------------+ |
| 68 | + |
| 69 | +-- Solution |
| 70 | +with t1 as( |
| 71 | +Select c.host_id, c.host_name, c.host_points |
| 72 | +from( |
| 73 | +select a.match_id, a.team_id as host_id, a.team_name as host_name, b.team_id as guest_id, b.team_name as guest_name, a.host_goals, a.guest_goals, |
| 74 | +case |
| 75 | +when a.host_goals > a.guest_goals then 3 |
| 76 | +when a.host_goals = a.guest_goals then 1 |
| 77 | +else 0 |
| 78 | +end as host_points, |
| 79 | +case |
| 80 | +when a.host_goals < a.guest_goals then 3 |
| 81 | +when a.host_goals = a.guest_goals then 1 |
| 82 | +else 0 |
| 83 | +end as guest_points |
| 84 | +from( |
| 85 | +select * |
| 86 | +from matches m |
| 87 | +join teams t |
| 88 | +on t.team_id = m.host_team) a |
| 89 | +join |
| 90 | +(select * |
| 91 | +from matches m |
| 92 | +join teams t |
| 93 | +on t.team_id = m.guest_team) b |
| 94 | +on a.match_id = b.match_id) c |
| 95 | +union all |
| 96 | +Select d.guest_id, d.guest_name, d.guest_points |
| 97 | +from( |
| 98 | +select a.match_id, a.team_id as host_id, a.team_name as host_name, b.team_id as guest_id, b.team_name as guest_name, a.host_goals, a.guest_goals, |
| 99 | +case |
| 100 | +when a.host_goals > a.guest_goals then 3 |
| 101 | +when a.host_goals = a.guest_goals then 1 |
| 102 | +else 0 |
| 103 | +end as host_points, |
| 104 | +case |
| 105 | +when a.host_goals < a.guest_goals then 3 |
| 106 | +when a.host_goals = a.guest_goals then 1 |
| 107 | +else 0 |
| 108 | +end as guest_points |
| 109 | +from( |
| 110 | +select * |
| 111 | +from matches m |
| 112 | +join teams t |
| 113 | +on t.team_id = m.host_team) a |
| 114 | +join |
| 115 | +(select * |
| 116 | +from matches m |
| 117 | +join teams t |
| 118 | +on t.team_id = m.guest_team) b |
| 119 | +on a.match_id = b.match_id) d) |
| 120 | + |
| 121 | +Select team_id, team_name, coalesce(total,0) as num_points |
| 122 | +from teams t2 |
| 123 | +left join( |
| 124 | +select host_id, host_name, sum(host_points) as total |
| 125 | +from t1 |
| 126 | +group by host_id, host_name) e |
| 127 | +on t2.team_id = e.host_id |
| 128 | +order by num_points desc, team_id |
0 commit comments