]> git.sesse.net Git - ccbs/blobdiff - sql/ccbs.sql
Move FetchMaxScoreForSongs into a stored procedure.
[ccbs] / sql / ccbs.sql
index 2b1a68605721d7ce334075fbb4873eff8a83221d..4c42d56187a4c01f185ead62367b10f081503ff5 100644 (file)
@@ -68,6 +68,8 @@ CREATE TABLE songratings (
 );
 CREATE INDEX songratings_feetrating ON songratings ( feetrating );
 
+CREATE VIEW max_single_feetrating AS SELECT machine,song,MAX(feetrating) AS feetrating FROM songratings WHERE playmode='single' GROUP BY machine,song;
+
 CREATE TABLE players (
        player SERIAL PRIMARY KEY,
        nick VARCHAR NOT NULL,
@@ -111,8 +113,7 @@ CREATE TABLE tournamentrankings (
        player INTEGER NOT NULL REFERENCES players,
        points INTEGER,
 
-       UNIQUE (tournament, player),
-       PRIMARY KEY (tournament, ranking)
+       PRIMARY KEY (tournament, player)
 );
 
 CREATE TABLE rounds (
@@ -199,3 +200,27 @@ CREATE TABLE bigscreen.active_groups (
 CREATE TABLE bigscreen.active_screens (
        id VARCHAR NOT NULL PRIMARY KEY
 );
+
+-- get_max_score_for_songs(tournament, playmode)
+CREATE TYPE max_score AS (
+       song INTEGER,
+       max_score INTEGER
+);
+
+CREATE FUNCTION get_max_score_for_songs(integer, varchar) RETURNS SETOF max_score
+    AS $$
+       SELECT
+           song,
+           MAX(feetrating)*1000 AS max_score
+       FROM songratings
+       WHERE
+           machine=(
+               SELECT machine FROM tournaments WHERE tournament=$1
+            )
+           AND playmode=$2
+       GROUP BY song
+       ;
+$$
+    LANGUAGE SQL
+    STABLE
+    RETURNS NULL ON NULL INPUT;