LeetCode_sql_day18(1841.联赛信息统计)

news2024/9/28 9:26:24

描述

表: 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 *,
                   case
                       when home_team_goals > away_team_goals then 3
                       when home_team_goals = away_team_goals then 1
                       when home_team_goals < away_team_goals then 0
                       end as home_team_points,
                   case
                       when home_team_goals < away_team_goals then 3
                       when home_team_goals = away_team_goals then 1
                       when home_team_goals > away_team_goals then 0
                       end as away_team_points
            from Matches

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

with t1 as (select *,
                   case
                       when home_team_goals > away_team_goals then 3
                       when home_team_goals = away_team_goals then 1
                       when home_team_goals < away_team_goals then 0
                       end as home_team_points,
                   case
                       when home_team_goals < away_team_goals then 3
                       when home_team_goals = away_team_goals then 1
                       when home_team_goals > away_team_goals then 0
                       end as away_team_points
            from 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_id
    union
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 *,
                   case
                       when home_team_goals > away_team_goals then 3
                       when home_team_goals = away_team_goals then 1
                       when home_team_goals < away_team_goals then 0
                       end as home_team_points,
                   case
                       when home_team_goals < away_team_goals then 3
                       when home_team_goals = away_team_goals then 1
                       when home_team_goals > away_team_goals then 0
                       end as away_team_points
            from 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 matches

union 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_against
 from 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2089092.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

StarShip v0.5版本更新

CodeSouler更新 IDE插件&#xff08;CodeSouler&#xff09; 01 代码补全优化 &#x1f680; 解决了Tab操作与IDE自带补全的冲突。 优化代码补全机制&#xff0c;调整触发逻辑并改进防抖算法&#xff0c;减少编码干扰。 修复了JetBrains插件中的多余 ) 和 } 符号问题。 02 代…

【GPT】Coze使用开放平台接口-【4】创建机器人

在前面三篇&#xff0c;我们分别创建了插件&#xff0c;插件里面添加了多个工具。接着&#xff0c;我们把插件添加到工作流内&#xff0c;成为一个开放平台API的调用节点&#xff0c;从而创建出一条业务流。分别是&#xff0c;语音伪造检测工作流&#xff0c;以及通话语音内容分…

day14JS-正则表达式

1. 什么是正则表达式 正则表达式(Regular Expression)是一种文本模式&#xff0c;包括普通字符&#xff08;例如&#xff0c;a 到 z 之间的字母&#xff09;和特殊字符&#xff08;称为"元字符"&#xff09;&#xff0c;可以用来描述和匹配字符串的特定模式。正则表…

SRA ToolKit(v 3.1.1)安装和使用(Bioinformatics tools-032)

01 检索数据 run就是数据&#xff0c;如SRR26717485 SRA 档案数据通过 SRA 加载过程进行标准化&#xff0c;并由 SRA 工具包用于读取和生成如 FASTQ、SAM 等格式。默认的工具包配置使其能够通过登录号查找和检索 SRA 运行数据。 现在&#xff0c;公共 SRA 文件可以通过 GCP 和…

[WCT系列(四):BLASTSyncEngine

WCT系列&#xff08;一&#xff09;&#xff1a;WindowContainerTransaction类详解 WCT系列&#xff08;二&#xff09;&#xff1a;SyncTransactionQueue类详解 WCT系列&#xff08;三&#xff09;&#xff1a;WindowOrganizerController WCT系列&#xff08;四&#xff09;&a…

分治,CF 768B. Code For 1

目录 一、题目 1、题目描述 2、输入输出 2.1输入 2.2输出 3、原题链接 二、解题报告 1、思路分析 2、复杂度 3、代码详解 一、题目 1、题目描述 2、输入输出 2.1输入 2.2输出 3、原题链接 https://codeforces.com/problemset/problem/768/B 二、解题报告 1、思路…

python-读取word中的内容

doc Document(rD:\xxxx\xxxx\xxx.docx) #读取word中所有内容 for p in doc.paragraphs print(p,p.text) #读取指定段落中的所有run(文字块) for run in doc.paragraphs[1].runs: print(run,run.text) #读取word中所有表格内容 for 表格 in doc.tables: print(表格) for 行 in …

什么是家庭全光组网和企业全光组网,两者有什么区别?

家庭全光组网和企业全光组网虽然都是基于光纤技术来实现高速网络连接&#xff0c;但它们在应用场景、规模、需求和技术细节上存在一些差异。 家庭全光组网 目标用户&#xff1a;面向个人家庭用户。 规模&#xff1a;一般为单个住宅内的网络覆盖&#xff0c;或者小范围内的多个房…

零基础泛微二开指南

前言 在泛微系统上开发一个自定义post接口 准备 首先准备工作要做好&#xff0c;安装一个泛微&#xff0c;之后所有的操作要在泛微的安装目录操作 参考官网安装&#xff0c;挺麻烦的&#xff1b; IDEA 1、直接新建项目 new ->Project from Existing Sources.直接打开泛…

uniapp微信小程序page-container导致滚动失效/向下偏移,返回上一页/左滑取消返回上一页

项目场景&#xff1a; 提示&#xff1a;这里简述项目相关背景&#xff1a; 前提&#xff1a; 使用uniapp来做的微信小程序 有两级tab页面 要求手机的两边往中间滑时 要求&#xff08;调用手机的物理返回按钮--有震动感&#xff09; 返回上一页。具体如下图箭头所示&#xf…

数据防泄漏软件10款超好用推荐|2024数据防泄漏软件排名

在2024年&#xff0c;数据防泄漏软件市场涌现了多款优秀的产品&#xff0c;它们通过不同的技术手段和策略&#xff0c;为企业提供全面的数据安全保护。以下是10款超好用的数据防泄漏软件推荐及简要排名。 1.安企神 特点&#xff1a;专为企业设计的数据安全防护工具&#xff0c…

【零知识证明】MiMC哈希函数电路

1 哈希电路 哈希函数电路实现&#xff1a; pragma circom 2.0.0;// y (x k c) ^ 5 // 输入信号x, k &#xff0c;常量c // base x k c // base2 base * base // base4 base2 * base2 // base5 base *base4 // 输出 ytemplate MIMC5(){signal input x;signal input k…

使用Aqua进行WebUI测试(Pytest)——介绍篇

一、在创建时选择Selenium with Pytest 如果选择的是Selenium&#xff0c;则只能选择Java类语言 选择selenium with Pytest&#xff0c;则可以选择Python类语言 Environment 其中的【Environment】可选New 和 Existing New &#xff1a;选择这个选项意味着你希望工具为你创…

【Go函数详解】二、参数传递、变长参数与多返回值

文章目录 一、传递参数1. 按值传参2. 引用传参2.1 特殊情况2.1.1 切片slice2.1.2 字典map 二、变长参数1. 基本定义和传值1.1 基本定义1.2 传值1.2.1 普通传值1.2.2 传递切片 2. 任意类型的变长参数&#xff08;泛型&#xff09; 三、多返回值1. 命名返回值 一、传递参数 1. 按…

破解电商数据分析难题,优化运营策略的秘诀

在电商行业中&#xff0c;数据分析是不可或缺的一部分。它能帮助商家精准掌握市场动态&#xff0c;优化运营策略&#xff0c;从而提升销售业绩。然而&#xff0c;面对大量复杂的数据&#xff0c;许多电商运营者往往不知道从哪里开始分析。那么&#xff0c;电商运营究竟如何有效…

优可测白光干涉仪助力红外探测行业发展——晶圆衬底检测

从18世纪红外线被发现&#xff0c;到19世纪红外探测器的发明。至今&#xff0c;随着工艺更新迭代&#xff0c;红外探测器朝着多波段、大面阵、高分辨率、低成本量产快速发展。 今天&#xff0c;小优博士带您探索红外探测的奥秘。 一、红外线是什么 红外光是一种电磁波&#x…

【 OpenHarmony 系统应用源码解析 】-- Launcher 初体验

前言 最近因为业务需要&#xff0c;需要做一款 UI 定制的鸿蒙 Launcher&#xff0c;于是就开始了「找到代码」、「研究代码」、「魔改代码」的套路流程&#xff0c;仅以此文章作为知识备份和技术探讨所用&#xff0c;也希望能给其他小伙伴提供一些源码的解析思路&#xff0c;方…

移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——8.stackqueuepriority_queue(模拟实现)

1.stack 可通过模板使用其他类来建立stack&#xff08;如vector&#xff0c;list&#xff09; #include<vector>namespace zone {template<class T,class container> //两个模板参数class stack{public:void push(const T& x){it.push_back(x); //使用it的p…

【Linux】命令简介------迅速掌握Linux命令

目录 Linux 命令 &#x1f354; ls命令 &#x1f354; cd 和 pwd命令 &#x1f354; 相对路径和绝对路径 &#x1f354; 文件/文件夹的创建以及文件内容的浏览 &#x1f354; 文件的复制,移动和删除 &#x1f354; 文件的查找 &#x1f354; grep 和管道 &#x1f354…

Windows11安装SqlLite、Navicat Premium 15连接SqlLite、Springboot集成SqlLite

一、Windows11安装SqlLite 1、下载安装包 地址&#xff1a;SQLite Download Page 2、压缩包解压 3、配置系统环境变量 4、验证安装是否成功 打开命令提示符&#xff0c;输入 sqlite3 5、创建数据库文件 新建文件重命名为你想要的数据库名称&#xff0c;文件后缀改为.db 二、…