对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 ...
随机推荐
- ImportError: No module named 'tkinter'
环境说明: windows7.vscode1.33.1.python3.7.0. 解决方案: 通过安装程序单独卸载“tcl/tk and IDLE”------重新安装“tcl/tk and IDLE ...
- 07-oracle多表查询
--笛卡尔积,多表查询时,n张表中的行数相乘(本例中14*4=56)--多表查询时笛卡尔积无法消除,即使使用了限定条件(where)也只是不显示而已,实际上笛卡尔积仍存在 --只能使用合理的做法来处理 ...
- js验证码有效时间倒计时
js验证码有效时间倒计时 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...
- vue相关问题在工作中的问题及ui组件及html样式搭建相关网站下载资源
https://youzan.github.io/vant/#/zh-CN/nav-bar http://www.builive.com/docs/api/index.html bui框架BUI ...
- nodejs 并发控制
1.用 eventproxy 实现控制并发: var EventProxy = require('eventproxy'); const most = 5;//并发数5 var urllist = [ ...
- 对Map的一些总结
1:Map接口. Collection体系中存储的是单个元素,单身汉,而Map中存储的是2个元素,存储的是成对的元素. Map和Collection是没有联系的!!不要以为Map是Collection ...
- 使用solr模拟京东搜素功能
1 项目需求 1.可以根据关键字搜索商品 2.可以根据商品的分类和价格过滤搜索结果 3.可以根据价格排序 4.可以实现基本的分页功能 2 界面效果 3 项目环境搭建 1.创建一个动态的web工程 2. ...
- springboot使用Freemarker继承
最近需要用到Freemarker的继承.但是发现没有关于springboot配置Freemarker的继承的.所以趁现在有时间写个博客. 1. Freemarker继承介绍 Freemarker 通过 ...
- Struts2入门介绍(二)
一.Struts执行过程的分析. 当我们在浏览器中输入了网址http://127.0.0.1:8080/Struts2_01/hello.action的时候,Struts2做了如下过程: 1.Stru ...
- Learning Linux Commands: awk--reference
http://how-to.linuxcareer.com/learning-linux-commands-awk 1. Introduction In this case, the title mi ...