-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path58.sql
37 lines (34 loc) · 1.03 KB
/
58.sql
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
DROP TABLE IF EXISTS Tickets;
CREATE TABLE Tickets
(buyer_name CHAR(5) NOT NULL,
ticket_nbr INTEGER DEFAULT 1 NOT NULL
CHECK (ticket_nbr > 0),
PRIMARY KEY (buyer_name, ticket_nbr));
INSERT INTO Tickets VALUES ('a', 2);
INSERT INTO Tickets VALUES ('a', 3);
INSERT INTO Tickets VALUES ('a', 4);
INSERT INTO Tickets VALUES ('b', 4);
INSERT INTO Tickets VALUES ('c', 1);
INSERT INTO Tickets VALUES ('c', 2);
INSERT INTO Tickets VALUES ('c', 3);
INSERT INTO Tickets VALUES ('c', 4);
INSERT INTO Tickets VALUES ('c', 5);
INSERT INTO Tickets VALUES ('d', 1);
INSERT INTO Tickets VALUES ('d', 6);
INSERT INTO Tickets VALUES ('d', 7);
INSERT INTO Tickets VALUES ('d', 9);
INSERT INTO Tickets VALUES ('e', 10);
WITH RECURSIVE r (buyer_name, ticket_nbr) AS (
SELECT buyer_name, 1 FROM Tickets GROUP BY buyer_name
UNION ALL
SELECT r.buyer_name, r.ticket_nbr + 1
FROM r
WHERE r.ticket_nbr < (
SELECT MAX(t.ticket_nbr)
FROM Tickets t
WHERE r.buyer_name = t.buyer_name
)
)
SELECT * FROM r
EXCEPT
SELECT * FROM Tickets;