对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 最后也把需要的数据筛选出来了吧 总结完毕 _(:з」∠)_

 

posted @ 2018-04-16 19:32  北方真白  阅读(1883)  评论(0编辑  收藏  举报