-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament.sql
41 lines (33 loc) · 923 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
-- Table definitions for the tournament project.
--
-- Put your SQL 'create table' statements in this file; also 'create view'
-- statements if you choose to use it.
--
-- You can write comments in this file by starting them with two dashes, like
-- these lines here.
DROP TABLE IF EXISTS match CASCADE;
DROP TABLE IF EXISTS player CASCADE;
CREATE TABLE player (
id SERIAL,
name TEXT,
created_by text DEFAULT current_user,
created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE match (
id SERIAL,
winner_id integer,
loser_id integer,
created_by text DEFAULT current_user,
created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE VIEW standings AS
SELECT
p.id as id,
p.name as name,
(SELECT count(*) FROM match m
WHERE p.id = m.winner_id) as wins,
(SELECT count(*) FROM match m
WHERE p.id = m.winner_id OR
p.id = m.loser_id) as matches
FROM player p
ORDER BY wins DESC;