Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
56
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. $$
  16.     DECLARE
  17.         r medal_type%rowtype;
  18.         null_test VARCHAR(32);
  19.         a_gold integer;
  20.         a_gold_time VARCHAR(32);
  21.         a_silver integer;
  22.         a_silver_time VARCHAR(32);
  23.         a_bronze integer;
  24.         a_bronze_time VARCHAR(32);
  25.     BEGIN
  26.         -- does this competition exist?
  27.         Select time into null_test from R_track where c = $1;
  28.         IF(null_test IS NULL) THEN
  29.             RAISE EXCEPTION 'Either track competition % does not exist or there were no participants in said competition thus far this year', $1;
  30.         END IF;
  31.         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');
  32.         r.medal_name = 'Gold';
  33.         r.a = a_gold;
  34.         r.time = a_gold_time;
  35.         return next r;
  36.         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);
  37.         r.medal_name = 'Silver';
  38.         r.a = a_silver;
  39.         r.time = a_silver_time;
  40.         return next r;
  41.         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);
  42.         r.medal_name = 'Bronze';
  43.         r.a = a_bronze;
  44.         r.time = a_bronze_time;
  45.         RETURN;
  46.     END
  47. $$
  48. LANGUAGE plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement