LeetCode-1212. Queries teams score (moderate)

Table: Teams

+ ---------- + --------------- +
| Column the Name | Type |
+ --------------- + ---------- +
| team_id | int |
| team_name | VARCHAR |
+ --------------- + ---------- +
the primary key of this table is team_id, each row in the table represents an independent football team.
Table: Matches

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| match_id | int |
| host_team | int |
| guest_team | int | 
| host_goals | int |
| guest_goals | int |
+ -------------- - + --------- +
primary key of this table is match_id, each row in the table represents a game has ended, the main game the visitors were represented by their own id, their goal by the host_goals and guest_goals respectively.
 

Points rules are as follows:

We have to win one-third;
flat was a one point;
lost a no points.
Write a SQL statement to query each team team_id, team_name and num_points. The results in descending order according to num_points, if two teams have the same points, the two teams team_id sorted in ascending order.

Query results in the following format:

Teams table:
+-----------+--------------+
| team_id   | team_name    |
+-----------+--------------+
| 10        | Leetcode FC  |
| 20        | NewYork FC   |
| 30        | Atlanta FC   |
| 40        | Chicago FC   |
| 50        | Toronto FC   |
+-----------+--------------+

Matches table:
+------------+--------------+---------------+-------------+--------------+
| match_id   | host_team    | guest_team    | host_goals  | guest_goals  |
+------------+--------------+---------------+-------------+--------------+
| 1          | 10           | 20            | 3           | 0            |
| 2          | 30           | 10            | 2           | 2            |
| 3          | 10           | 50            | 5           | 1            |
| 4          | 20           | 30            | 1           | 0            |
| 5          | 50           | 30            | 1           | 0            |
+------------+--------------+---------------+-------------+--------------+

Result table:
+------------+--------------+---------------+
| team_id    | team_name    | num_points    |
+------------+--------------+---------------+
| 10         | Leetcode FC  | 7             |
| 20         | NewYork FC   | 3             |
| 50         | Toronto FC   | 3             |
| 30         | Atlanta FC   | 1             |
| 40         | Chicago FC   | 0             |
+------------+--------------+---------------+

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/team-scores-in-football-tournament
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation: write a SQL statement to query each team team_id, team_name and num_points. The results in descending order according to num_points, if two teams have the same points, the two teams team_id sorted in ascending order.

The goals, the score is calculated, the final summation.

Consideration: First, based on a result of the data processing, calculating the game home team visiting team integration, then find all teams and scores id.

Problem solving:

Method One: ideas: seeking id, team name, score.

First, by union all the results of the competition table, the result will be two teams, together. If five games, the equivalent of 10 teams, swap the left and right sides of the table game, it may be the results of ten teams five games are included.

Then left join table teams that certainly more than the left to the right, back on the conditions determined by the comparison, to the right in each row of the packet is determined by comparing the integral, summation.

Finally, you can sort according to the conditions.

select t.team_id,t.team_name,
sum(case when m.host_goals > m.guest_goals then 3 
      when m.host_goals = m.guest_goals then 1
      else 0 end) num_points
from teams t 
left join
(select host_team, guest_team, host_goals, guest_goals
from matches
union all
select guest_team host_team, host_team guest_team, guest_goals host_goals, host_goals guest_goals
from matches) m
on m.host_team = t.team_id
group by t.team_id
order by num_points desc, t.team_id asc

-- 练习
-- left join 
-- LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹-- -- 配,则结果为 NULL。

select t.team_id, t.team_name,
sum(case when m.host_goals>m.guest_goals then 3 when m.host_goals = m.guest_goals then 1 else 0 end) num_points from teams t left join
(select host_team, guest_team, host_goals, guest_goals
from matches
union all
select guest_team host_team, host_team guest_team, guest_goals host_goals, host_goals guest_goals
from matches) m
on m.host_team = t.team_id
group by t.team_id
order by num_points desc,t.team_id asc;




Method Two:

select t.team_id,t.team_name,sum(
    CASE WHEN t.team_id= m.host_team and m.host_goals>m.guest_goals THEN 3
         WHEN m.host_goals = m.guest_goals THEN 1
         WHEN t.team_id = m.guest_team and m.guest_goals>m.host_goals THEN 3 ELSE 0 END
    ) as num_points 
from Teams t 
left join Matches m 
on t.team_id=m.host_team or t.team_id=m.guest_team 
group by t.team_id 
order by num_points desc ,team_id asc

Knowledge points:

UNION only select a different value. Use UNION ALL to select the duplicate values!

Published 144 original articles · won praise 2 · Views 5745

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104712880