对sql作业的总结(不用group by 通过with as,exists实现分类)
一次数据库作业 题目如下:
Consider the following SQL table definitions:
CREATE TABLE OlympicEvent (
Name text,
Year int,
Description text
);
CREATE TABLE Athlete (
Name text,
DateOfBirth date,
Gender char,
Nationality text
);
1. Define sensible key constraints for these tables in SQL. Note: An olympic event (such as 200m sprint) can take place in multiple years. Different athletes may have identical names, but should be distinguishable by date of birth. [1 mark]
2. Define an SQL constraint that restricts Gender to sensible values. [1 mark]
3. Create the SQL definition for a table Competes recording medals won by athletes competing in Olympic events. Define sensible key and foreign key constraints, as well as a constraint which ensures that medals can only be gold, silver or bronze. Note: While it can happen (in the case of ties) that multiple gold or silver medals are handed out for the same event, an athlete cannot gain multiple medals for the same event.[3 marks]
4. Write an SQL query returning all nations (nationalities of athletes) that won at least 2 gold medals in 2018. Do not use aggregation (GROUP BY). Note: Each nation satisfying the criteria should be listed only once.[3 marks]
5. Express the same query using aggregation. Submit your answers as pdf file via stream. Include your name and student ID.[2 marks]
对于前3问来讲,无非就是建表再加限制
CREATE TABLE OlympicEvent (
Name VARCHAR(20),
Year int,
Description text,
CONSTRAINT pk_OlympicEvent PRIMARY KEY (Name,Year)
);
CREATE TABLE Athlete (
Name VARCHAR(20),
DateOfBirth date,
Gender char(20) CHECK (Gender ='male' OR Gender = 'female'),
Nationality text,
CONSTRAINT pk_Athlete PRIMARY KEY (Name,DateOfBirth)
);
CREATE TABLE Competes (
Athlete_name VARCHAR(20),
DateOfBirth date,
OlympicEvent_name VARCHAR(20),
OlympicEvent_year int,
Medal CHAR(20) CHECK (Medal = 'gold' OR Medal = 'silver' OR Medal = 'bronze'),
CONSTRAINT fk_Athelete FOREIGN KEY (Athlete_name,DateOfBirth) REFERENCES Athlete(Name,DateOfBirth),
CONSTRAINT fk_OlympicEvent FOREIGN KEY (OlympicEvent_name,OlympicEvent_year) REFERENCES OlympicEvent(Name, Year)
);
然后往里面加数据
athlete:
olympicEvent表:
competes表:
第5问的话 联表后再用group by 也不是很难
SELECT athlete.Nationality ,count(1) as sum_medals FROM athlete,competes WHERE competes.Athlete_name = athlete.Name GROUP BY athlete.Nationality HAVING count(1)>=2;
结果如下
第4问 题目中不让用group by 问了问老师 也不让用sum count之类的聚集函数
我写的代码如下
WITH medal_nationality AS (
SELECT athlete_name, athlete.dateofbirth, olympicevent_year, olympicevent_name, nationality FROM athlete, competes
WHERE athlete.Name = competes.Athlete_name AND athlete.DateOfBirth = competes.DateOfBirth
) SELECT DISTINCT nationality FROM medal_nationality t1
WHERE EXISTS(
SELECT * FROM medal_nationality t2
WHERE t1.nationality = t2.nationality AND
(t1.athlete_name, t1.dateofbirth, t1.olympicevent_name, t1.olympicevent_year) <>
(t2.athlete_name, t2.dateofbirth, t2.olympicevent_name, t2.olympicevent_year)
);
利用with as 将medal_nationality选出来 包含 athlete_name, athlete.dateofbirth, olympicevent_year, olympicevent_name, nationality 5个字段,后用exists来选出只返回true的值,条件是必须国籍相等,但两个人不能是同一个人(见问题1),最后结果为:
算是不用group by 最后也把需要的数据筛选出来了吧 总结完毕 _(:з」∠)_
对sql作业的总结(不用group by 通过with as,exists实现分类)的更多相关文章
- 如何跑通第一个 SQL 作业
简介: 本文由阿里巴巴技术专家周凯波(宝牛)分享,主要介绍如何跑通第一个SQL. 一.SQL的基本概念 1.SQL 分类 SQL分为四类,分别是数据查询语言(DQL).数据操纵语言(DML).数据定义 ...
- sql中的 where 、group by 和 having 用法解析
--sql中的 where .group by 和 having 用法解析 --如果要用到group by 一般用到的就是“每这个字” 例如说明现在有一个这样的表:每个部门有多少人 就要用到分组的技术 ...
- Linux 上配置 SQL Server Always On Availability Group
SQL Server Always On Availability Group 配置步骤:配置三台 Linux 集群节点创建 Availability Group配置 Cluster Resource ...
- Ubuntu上配置SQL Server Always On Availability Group(Configure Always On Availability Group for SQL Server on Ubuntu)
下面简单介绍一下如何在Ubuntu上一步一步创建一个SQL Server AG(Always On Availability Group),以及配置过程中遇到的坑的填充方法. 目前在Linux上可以搭 ...
- Configure Always On Availability Group for SQL Server on RHEL——Red Hat Enterprise Linux上配置SQL Server Always On Availability Group
下面简单介绍一下如何在Red Hat Enterprise Linux上一步一步创建一个SQL Server AG(Always On Availability Group),以及配置过程中遇到的坑的 ...
- MS SQL作业Schedule的限制注意事项
最近遇到了一个关于MS SQL作业Schedule下有限制的特殊案例,有一个作业,用户要求执行的时间为:9:30,14:30,16:30, 19:00,于是我设置了两个Schedule,其中一个每 ...
- 转:sql篇 select from where group by having order by
原文地址: sql篇 select from where group by having order by select from where group by having order by 的基 ...
- Configure Always On Availability Group for SQL Server on Ubuntu——Ubuntu上配置SQL Server Always On Availability Group
下面简单介绍一下如何在Ubuntu上一步一步创建一个SQL Server AG(Always On Availability Group),以及配置过程中遇到的坑的填充方法. 目前在Linux上可以搭 ...
- 查看sql 作业明细及运行记录
--查看作业明细及状态 select j.name 'Job名', j.description '描述', j.ENABLED job_enabled, cast(js.last_run_date a ...
随机推荐
- BAE+Python+Django+Wechatpy+Baidu weather api +微信订阅号 = 实现微信查询天气
最近想在微信上面实现天气查询,分两个步骤: 1.被动回复:输入天气.xx天气获取天气情况 2.主动推送:每天定时推送天气(针对24小时内产生交互的人) 目前已经实现第一个步骤,如下: 现将实现此功能环 ...
- php+windows环境安装
1.下载并安装phpstorm 2.查找激活码激活phpstorm 3.由于php官方没有提供exe版本,安装phpstudy,获得windows下exe编译版本的php 4.安装VisualSVN ...
- CSS生成小三角
前言:小三角的应用场景:鼠标移动到某个按钮上面,查看信息详情时,信息详情弹出框有时候会需要一个小三角. 代码如下: <div id='triangle'></div> #tri ...
- Starting vsftpd for vsftpd: [FAILED]问题的解决
问题描述 [root@bigdatamaster etc]# rpm -qa| grep vsftpd vsftpd--.el6.x86_64 [root@bigdatamaster etc]# [r ...
- java中String,StringBuffer与StringBuilder的区别??
本文着重介绍下,应该在何时恰当的使用string,stringbuffer,stringbuilder. 1,执行速度 StringBuilder > StringBuffer > ...
- UML 依赖\泛化\关联\实现\聚合\组合的 Java实现
在类图中,类与类之间的关系主要有一下几种: 泛化关系:(就是继承) public class Employee { } public class SaleEmployee extends Employ ...
- MySQL存储过程中判断形参是否为空null
直接看例子: DELIMITER $$CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `restore`(username varchar(50))BEGINi ...
- 游戏场景下的DDoS风险分析及防护
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 作者:腾讯游戏云资深架构师 vince 本篇文章主要是分享游戏业务面临的安全风险场景,以及基于这些场景的特点,我们应该如何做好对应的防护. ...
- 禅道和JIRA大对比
转自:https://blog.csdn.net/qq_40543535/article/details/78182636?locationNum=9&fps=1 禅道和JIRA大对比 置顶 ...
- C# WinForm API 改进单实例运行
在普通的单实例中,第二次点击软件快捷方式的时候,往往简单提示"系统已经运行",而不是把第一次打开的软件主窗体显示出来,下面演示如果主窗体已经打开则把第一次打开的主窗体放置到最前面; ...