db views now enumerate rows
This commit is contained in:
parent
684ded6af0
commit
72bb54a536
4 changed files with 45 additions and 12 deletions
32
backend/db/initScripts/_createSchema.sql
Normal file
32
backend/db/initScripts/_createSchema.sql
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
CREATE TABLE "user" (
|
||||
username VARCHAR(32) PRIMARY KEY
|
||||
);
|
||||
|
||||
CREATE TABLE game (
|
||||
id SERIAL PRIMARY KEY,
|
||||
score INTEGER NOT NULL,
|
||||
playtime TIME NOT NULL,
|
||||
date DATE NOT NULL,
|
||||
username VARCHAR(32) NOT NULL,
|
||||
FOREIGN KEY (username) REFERENCES "user"
|
||||
);
|
||||
|
||||
CREATE VIEW user_data AS (
|
||||
SELECT
|
||||
username,
|
||||
max(score) AS highscore,
|
||||
sum(score) AS total_score,
|
||||
sum(playtime) AS total_playtime,
|
||||
avg(score) AS average_score,
|
||||
count(*) AS games_played
|
||||
FROM game
|
||||
GROUP BY username
|
||||
);
|
||||
|
||||
CREATE VIEW lb_highscore AS (
|
||||
SELECT row_number() OVER (ORDER BY max(score) DESC) AS rank, username, max(score) AS highscore FROM game GROUP BY username ORDER BY rank
|
||||
);
|
||||
|
||||
CREATE VIEW lb_total_playtime AS (
|
||||
SELECT row_number() OVER (ORDER BY sum(playtime) DESC) AS rank, username, sum(playtime) AS total_playtime FROM game GROUP BY username ORDER BY rank
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue