Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. \connect sportsdb
  2.  
  3. CREATE TYPE MEDAL_TYPE AS (
  4.     medal_name VARCHAR(10),
  5.     A INTEGER,
  6.     Time VARCHAR(32)
  7. );
  8.  
  9. --create table R_TRACK (
  10. --       C VARCHAR(32) references TRACK(C),
  11. --       A INTEGER references ATHLETES(A), /* VARCHAR(5)--> INTEGER */
  12. --       Time VARCHAR(32));
  13.  
  14. CREATE OR REPLACE FUNCTION get_medals(c_target VARCHAR) RETURNS setof medal_type as
  15.     DECLARE
  16.         r medal_type%rowtype;
  17.         null_test varchar;
  18.         a_gold integer;
  19.         a_gold_time integer;
  20.         a_silver integer;
  21.         a_silver_time integer;
  22.         a_bronze integer;
  23.         a_bronze_time integer;
  24.     BEGIN
  25.         -- does this competition exist?
  26.         Select time into null_test from R_track where c = $1;
  27.         IF(null_test IS NULL) THEN
  28.             RAISE EXCEPTION 'Either track competition % does not exist or there were no participants in said competition thus far this year', $1;
  29.         END IF;
  30.         Select a into a_gold, time into a_gold_time from R_track where time in (select min(time) from R_track where c=$1 and time <> '000:00:00');
  31.         r.medal_name = 'Gold';
  32.         r.a = a_gold;
  33.         r.time = a_gold_time;
  34.         return next r;
  35.         Select a into a_silver, time into a_silver_time from R_track where time in (select min(time) from R_track where c=$1 and time <> '000:00:00' and a <> a_gold and time <> a_gold_time);
  36.         r.medal_name = 'Silver';
  37.         r.a = a_silver;
  38.         r.time = a_silver_time;
  39.         return next r;
  40.         Select a into a_bronze, time into a_bronze_time from R_track where time in (select min(time) from R_track where c=$1 and time <> '000:00:00' and a <> a_gold and a <> a_silver and time <> a_gold_time and time <> a_silver_time);
  41.         r.medal_name = 'Bronze';
  42.         r.a = a_bronze;
  43.         r.time = a_bronze_time;
  44.         RETURN;
  45.     END
  46. LANGUAGE plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement