zoukankan      html  css  js  c++  java
  • 1212. Team Scores in Football Tournament

    Table: Teams

    +---------------+----------+
    | Column Name | Type |
    +---------------+----------+
    | team_id | int |
    | team_name | varchar |
    +---------------+----------+
    team_id is the primary key of this table.
    Each row of this table represents a single football team.
    Table: Matches

    +---------------+---------+
    | Column Name | Type |
    +---------------+---------+
    | match_id | int |
    | host_team | int |
    | guest_team | int |
    | host_goals | int |
    | guest_goals | int |
    +---------------+---------+
    match_id is the primary key of this table.
    Each row is a record of a finished match between two different teams.
    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.
     

    You would like to compute the scores of all teams after all matches. Points are awarded as follows:
    A team receives three points if they win a match (Score strictly more goals than the opponent team).
    A team receives one point if they draw a match (Same number of goals as the opponent team).
    A team receives no points if they lose a match (Score less goals than the opponent team).
    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).

    The query result format is in the following example:

    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 |
    +------------+--------------+---------------+

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/team-scores-in-football-tournament
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    # Write your MySQL query statement below

    select t1.team_id, t1.team_name, ifnull(t3.num_points, 0) as num_points from Teams t1 left join 
    (
    select t2.team_id, sum(t2.num_points) as num_points from
    (
     select m.host_team as team_id, sum(if(host_goals>guest_goals, 3, if(host_goals=guest_goals,1,0))) as num_points from Matches m group by host_team
     union all
      select m.guest_team as team_id, sum(if(host_goals<guest_goals, 3, if(host_goals=guest_goals,1,0))) as num_points from Matches m group by guest_team
    ) t2 group by t2.team_id ) t3  on t1.team_id = t3.team_id order by num_points desc, t1.team_id
  • 相关阅读:
    初学C++到底应该用什么工具比较合适——工具简析
    便携式办公套件LibreOffice Portable 4.0.1
    Hibernate和JPA之间的联系
    央视《家有妙招》整理版,共250招,值得永远收藏
    思科Vs华为:不可避免的对决
    Facebook手机刺激了谁?
    Facebook利用Home平台加速进军移动领域
    英特尔Haswell处理器已出货 预计6月推出
    苹果自建街景地图 或与谷歌针锋相对
    图片链接
  • 原文地址:https://www.cnblogs.com/leeeee/p/11902053.html
Copyright © 2011-2022 走看看