LeetCode_sql_day18(1841.联赛信息统计)

2024-08-31 01:52

本文主要是介绍LeetCode_sql_day18(1841.联赛信息统计),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

描述

表: Teams

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| team_id        | int     |
| team_name      | varchar |
+----------------+---------+
team_id 是该表主键.
每一行都包含了一个参加联赛的队伍信息.

表: Matches

+-----------------+---------+
| Column Name     | Type    |
+-----------------+---------+
| home_team_id    | int     |
| away_team_id    | int     |
| home_team_goals | int     |
| away_team_goals | int     |
+-----------------+---------+
(home_team_id, away_team_id) 是该表主键.
每一行包含了一次比赛信息.
home_team_goals 代表主场队得球数.
away_team_goals 代表客场队得球数.
获得球数较多的队伍为胜者队伍.

写一段SQL,用来报告联赛信息. 统计数据应使用已进行的比赛来构建,其中 获胜 球队获得 三分 ,而失败球队获得 零分 。如果 打平 ,两支球队都得 一分 

result 表的每行应包含以下信息:

  • team_name - Teams 表中的队伍名字
  • matches_played - 主场与客场球队进行的比赛次数.
  • points - 球队获得的总分数.
  • goal_for - 球队在所有比赛中获取的总进球数
  • goal_against - 球队在所有比赛中,他的对手球队的所有进球数
  • goal_diff - goal_for - goal_against.

按 points 降序 返回结果表。 如果两队或多队得分相同,则按 goal_diff 降序 排列。 如果仍然存在平局,则以 team_name 按字典顺序 排列它们。

查询的结果格式如下例所示。

示例 1:

输入:
Teams 表:
+---------+-----------+
| team_id | team_name |
+---------+-----------+
| 1       | Ajax      |
| 4       | Dortmund  |
| 6       | Arsenal   |
+---------+-----------+
Matches 表:
+--------------+--------------+-----------------+-----------------+
| home_team_id | away_team_id | home_team_goals | away_team_goals |
+--------------+--------------+-----------------+-----------------+
| 1            | 4            | 0               | 1               |
| 1            | 6            | 3               | 3               |
| 4            | 1            | 5               | 2               |
| 6            | 1            | 0               | 0               |
+--------------+--------------+-----------------+-----------------+
输出:
+-----------+----------------+--------+----------+--------------+-----------+
| team_name | matches_played | points | goal_for | goal_against | goal_diff |
+-----------+----------------+--------+----------+--------------+-----------+
| Dortmund  | 2              | 6      | 6        | 2            | 4         |
| Arsenal   | 2              | 2      | 3        | 3            | 0         |
| Ajax      | 4              | 2      | 5        | 9            | -4        |
+-----------+----------------+--------+----------+--------------+-----------+
解释:
Ajax (team_id=1) 有4场比赛: 2败2平. 总分数 = 0 + 0 + 1 + 1 = 2.
Dortmund (team_id=4) 有2场比赛: 2胜. 总分数 = 3 + 3 = 6.
Arsenal (team_id=6) 有2场比赛: 2平. 总分数 = 1 + 1 = 2.
Dortmund 是积分榜上的第一支球队. Ajax和Arsenal 有同样的分数, 但Arsenal的goal_diff高于Ajax, 所以Arsenal在表中的顺序在Ajaxzhi'qian.

数据准备

Create table If Not Exists Teams (team_id int, team_name varchar(20))
Create table If Not Exists Matches
(home_team_id    int,away_team_id    int,home_team_goals int,away_team_goals int
)
Truncate table Teams ;
insert into Teams (team_id, team_name)
values ('1', 'Ajax')
insert
into Teams (team_id, team_name)
values ('4', 'Dortmund')
insert into Teams (team_id, team_name)
values ('6', 'Arsenal');
Truncate table Matches;
insert into Matches (home_team_id, away_team_id, home_team_goals, away_team_goals)
values ('1', '4', '0', '1')
insert into Matches (home_team_id, away_team_id, home_team_goals, away_team_goals)
values ('1', '6', '3', '3')
insert into Matches (home_team_id, away_team_id, home_team_goals, away_team_goals)
values ('4', '1', '5', '2')
insert into Matches (home_team_id, away_team_id, home_team_goals, away_team_goals)
values ('6', '1', '0', '0');

分析

①先构造出得分情况

select *,casewhen home_team_goals > away_team_goals then 3when home_team_goals = away_team_goals then 1when home_team_goals < away_team_goals then 0end as home_team_points,casewhen home_team_goals < away_team_goals then 3when home_team_goals = away_team_goals then 1when home_team_goals > away_team_goals then 0end as away_team_pointsfrom Matches

②然后分别计算球队比赛次数(主队的次数+客队的次数)、球队总得分(主队时的得分+客队时的得分)、球队总进球数(主队时的总进球数+客队时的总进球数)、对手总进球数(作为主队时对手作为客队的进球数+作为客队时对手作为主队的总进球数)

with t1 as (select *,casewhen home_team_goals > away_team_goals then 3when home_team_goals = away_team_goals then 1when home_team_goals < away_team_goals then 0end as home_team_points,casewhen home_team_goals < away_team_goals then 3when home_team_goals = away_team_goals then 1when home_team_goals > away_team_goals then 0end as away_team_pointsfrom Matches)
select distinct team_name,(select count(1) from t1 where home_team_id = Matches.home_team_id or away_team_id =Matches.home_team_id)  as matches_played,(select sum(home_team_points) from t1 where home_team_id = Matches.home_team_id) +(select sum(away_team_points) from t1 where away_team_id = Matches.home_team_id) as points,(select sum(home_team_goals) from t1 where home_team_id = Matches.home_team_id) +(select sum(away_team_goals) from t1 where away_team_id = Matches.home_team_id) as goal_for,(select sum(away_team_goals) from t1 where home_team_id = Matches.home_team_id) +(select sum(home_team_goals) from t1 where away_team_id = Matches.home_team_id) as goal_against
from matches , teams where matches.home_team_id = teams.team_idunion
select distinct team_name,(select count(1) from t1 where away_team_id = Matches.away_team_id or home_team_id =Matches.away_team_id)  as matches_played,(select ifnull(sum(home_team_points),0 ) from t1 where home_team_id = Matches.away_team_id) +(select ifnull(sum(away_team_points),0) from t1 where away_team_id = Matches.away_team_id) as points,(select ifnull(sum(home_team_goals),0) from t1 where home_team_id = Matches.away_team_id) +(select ifnull(sum(away_team_goals),0) from t1 where away_team_id = Matches.away_team_id) as goal_for,(select ifnull(sum(away_team_goals),0) from t1 where home_team_id = Matches.away_team_id) +(select ifnull(sum(home_team_goals),0) from t1 where away_team_id = Matches.away_team_id) as goal_against
from matches , teams where matches.away_team_id = teams.team_id

③基于上述结果 求goal_diff并且按照题目要求排序

select          team_name,matches_played,points,goal_for,goal_against,(goal_for-goal_against) as goal_diff from t2
order by points desc,goal_diff desc,team_name desc;

图解:

输入
home_team_idaway_team_idhome_team_goalsaway_team_goalshome_team_pointsaway_team_pointsteam_idteam_name
1401034Dortmund
1633111Ajax
4152306Arsenal
610011
分别求出各队作为
主队和客队时的分数、球数
结果
team_namematches_playedpointsgoal_forgoal_against
结果(最终)Dortmund2662
主队的+客队的主队的+客队的主队的+客队的主队的+客队的主队的+客队的Arsenal2233
Ajax4259
在此基础上求出goal_diff
team_namematches_playedpointsgoal_forgoal_againstgoal_diff
Dortmund26624
Arsenal22330
Ajax4259-4

代码

with t1 as (select *,casewhen home_team_goals > away_team_goals then 3when home_team_goals = away_team_goals then 1when home_team_goals < away_team_goals then 0end as home_team_points,casewhen home_team_goals < away_team_goals then 3when home_team_goals = away_team_goals then 1when home_team_goals > away_team_goals then 0end as away_team_pointsfrom Matches)
, t2 as (
select home_team_id,(select count(1) from t1 where home_team_id = Matches.home_team_id or away_team_id =Matches.home_team_id)             as matches_played,(select sum(home_team_points) from t1 where home_team_id = Matches.home_team_id) +(select sum(away_team_points) from t1 where away_team_id = Matches.home_team_id) as points,(select sum(home_team_goals) from t1 where home_team_id = Matches.home_team_id) +(select sum(away_team_goals) from t1 where away_team_id = Matches.home_team_id) as goal_for,(select sum(away_team_goals) from t1 where home_team_id = Matches.home_team_id) +(select sum(home_team_goals) from t1 where away_team_id = Matches.home_team_id) as goal_against
#         goal_for-goal_against as goal_diff
from matchesunion all
(select away_team_id,(select count(1) from t1 where away_team_id = Matches.away_team_id or home_team_id =Matches.away_team_id)             as matches_played,(select sum(away_team_points) from t1 where away_team_id = Matches.away_team_id) +(select sum(home_team_points) from t1 where home_team_id = Matches.away_team_id) as points,(select sum(home_team_goals) from t1 where home_team_id = Matches.away_team_id) +(select sum(away_team_goals) from t1 where away_team_id = Matches.away_team_id) as goal_for,(select sum(away_team_goals) from t1 where home_team_id = Matches.away_team_id) +(select sum(home_team_goals) from t1 where away_team_id = Matches.away_team_id) as goal_againstfrom Matches)
)select distinct (select team_name from teams where team_id=t2.home_team_id)team_name,matches_played,points,goal_for,goal_against,(goal_for-goal_against) as goal_diff from t2
order by points desc,goal_diff desc,team_name;

总结

最后要考虑到有的球队只有客队场 所以使用union 既要关联到主队id又要关联到客队id

这篇关于LeetCode_sql_day18(1841.联赛信息统计)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1122552

相关文章

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

2025版mysql8.0.41 winx64 手动安装详细教程

《2025版mysql8.0.41winx64手动安装详细教程》本文指导Windows系统下MySQL安装配置,包含解压、设置环境变量、my.ini配置、初始化密码获取、服务安装与手动启动等步骤,... 目录一、下载安装包二、配置环境变量三、安装配置四、启动 mysql 服务,修改密码一、下载安装包安装地

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

MySQL多实例管理如何在一台主机上运行多个mysql

《MySQL多实例管理如何在一台主机上运行多个mysql》文章详解了在Linux主机上通过二进制方式安装MySQL多实例的步骤,涵盖端口配置、数据目录准备、初始化与启动流程,以及排错方法,适用于构建读... 目录一、什么是mysql多实例二、二进制方式安装MySQL1.获取二进制代码包2.安装基础依赖3.清

详解MySQL中JSON数据类型用法及与传统JSON字符串对比

《详解MySQL中JSON数据类型用法及与传统JSON字符串对比》MySQL从5.7版本开始引入了JSON数据类型,专门用于存储JSON格式的数据,本文将为大家简单介绍一下MySQL中JSON数据类型... 目录前言基本用法jsON数据类型 vs 传统JSON字符串1. 存储方式2. 查询方式对比3. 索引