Advertisement
Guest User

Untitled

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