-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.sql
More file actions
39 lines (29 loc) · 1.3 KB
/
q1.sql
File metadata and controls
39 lines (29 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-- Q1. Airlines
-- You must not change the next 2 lines or the table definition.
SET SEARCH_PATH TO air_travel, public;
DROP TABLE IF EXISTS q1 CASCADE;
CREATE TABLE q1 (
pass_id INT,
name VARCHAR(100),
airlines INT
);
-- Do this for each of the views that define your intermediate steps.
-- (But give them better names!) The IF EXISTS avoids generating an error
-- the first time this file is imported.
DROP VIEW IF EXISTS intermediate_step CASCADE;
-- Define views for your intermediate steps here:
DROP VIEW IF EXISTS atleastone CASCADE;
CREATE VIEW atleastone AS
SELECT passenger.id AS pass_id, firstname||' '||surname AS name, count(DISTINCT booking.flight_id) AS airlines
FROM passenger, departure, booking
WHERE passenger.id = booking.pass_id and departure.flight_id = booking.flight_id and now()>departure.datetime
GROUP BY passenger.id;
DROP VIEW IF EXISTS noflights CASCADE;
CREATE VIEW noflights AS
SELECT passenger.id AS pass_id, firstname||' '||surname AS name, 0 AS airlines
FROM passenger, departure, booking
WHERE passenger.id = booking.pass_id and departure.flight_id = booking.flight_id and now()<=departure.datetime
GROUP BY passenger.id;
-- Your query that answers the question goes below the "insert into" line:
INSERT INTO q1
SELECT * FROM atleastone UNION SELECT * FROM noflights;