-
Notifications
You must be signed in to change notification settings - Fork 0
/
tournament.sql
42 lines (30 loc) · 910 Bytes
/
tournament.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
38
39
40
41
42
DROP DATABASE IF EXISTS tournament;
CREATE DATABASE tournament;
\c tournament;
DROP TABLE IF EXISTS player_rec;
CREATE TABLE player_rec(
id SERIAL PRIMARY KEY,
name TEXT UNIQUE
);
DROP TABLE IF EXISTS match_rec;
CREATE TABLE match_rec(
id SERIAL PRIMARY KEY,
winner INTEGER, FOREIGN KEY(winner)
REFERENCES player_rec(id),
loser INTEGER, FOREIGN KEY(loser)
REFERENCES player_rec(id)
);
DROP VIEW IF EXISTS standings_view;
CREATE VIEW standings_view AS
SELECT player_rec.id, player_rec.name,
(SELECT COUNT(match_rec.winner)
FROM match_rec
WHERE player_rec.id = match_rec.winner)
AS wins,
(SELECT COUNT(match_rec.id)
FROM match_rec
WHERE player_rec.id = match_rec.winner
OR player_rec.id = match_rec.loser)
AS matches
FROM player_rec
ORDER BY wins,matches;