daily pastebin goal
50%
SHARE
TWEET

Untitled

a guest May 17th, 2018 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION st() RETURNS varchar(255) AS $$
  2. DECLARE
  3.     cur CURSOR FOR SELECT station_id, train_id, arrive_time, departure_time FROM schedules;
  4.     cur2 CURSOR FOR SELECT station_id, train_id, arrive_time, departure_time FROM schedules;
  5.     curmax integer;
  6.     maximum integer;
  7.     maxstation varchar(255);
  8.     _count integer;
  9.     _station_name varchar(255);
  10.     _station_name2 varchar(255);
  11.     _train_code varchar(50);
  12.     _train_code2 varchar(50);
  13.     _arrive_time time;
  14.     _arrive_time2 time;
  15.     _departure_time time;
  16.     _departure_time2 time;
  17. BEGIN
  18.     OPEN cur;
  19.     OPEN cur2;
  20.     execute 'SELECT count(*) FROM schedules' into _count;  
  21.     maximum := 1;
  22.     maxstation := 'Везде равно';
  23.    
  24.     FOR i IN 1.._count LOOP
  25.         curmax := 1;
  26.         FETCH NEXT FROM cur INTO _station_name, _train_code, _arrive_time, _departure_time;
  27.         IF _arrive_time = null OR _departure_time = null THEN
  28.             CONTINUE;
  29.         END IF;
  30.         FOR j IN 1.._count LOOP
  31.             FETCH NEXT FROM cur2 INTO _station_name2, _train_code2, _arrive_time2, _departure_time2;
  32.             IF _arrive_time2 = null OR _departure_time = null THEN
  33.                 CONTINUE;
  34.             END IF;
  35.             IF _train_code <> _train_code2 THEN
  36.                 IF _arrive_time <= _arrive_time2 AND _departure_time >= _arrive_time2 THEN
  37.                     curmax := curmax + 1;
  38.                 ELSIF _arrive_time <= _departure_time2 AND _departure_time >= _departure_time2 THEN
  39.                     curmax := curmax + 1;
  40.                 END IF;
  41.             END IF;
  42.         END LOOP;
  43.         IF curmax > maximum THEN
  44.             maximum := curmax;
  45.             maxstation := _station_name;
  46.         END IF;
  47.     END LOOP;
  48.     return maxstation;
  49. END
  50. $$ LANGUAGE plpgsql;
  51.  
  52. SELECT st();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top