SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. drop function if exists stat();
  2. create function stat() returns table(min_id bigint, group_id bigint, counter bigint) as $$
  3. declare
  4.     item users;
  5.     prev_group_id bigint;
  6. begin
  7.     counter := 0;
  8.     for item.id, item.group_id in select users.id, users.group_id from users order by users.id asc
  9.     loop
  10.         if(prev_group_id != item.group_id and prev_group_id is not null ) then
  11.             return next;
  12.             counter := 0;
  13.             min_id := null;
  14.         end if;
  15.  
  16.         group_id := item.group_id;
  17.         counter := counter + 1;
  18.         if(item.id < min_id or min_id is null) then
  19.             min_id := item.id;
  20.         end if;
  21.         prev_group_id := item.group_id;
  22.     end loop;
  23.     return next;
  24. end
  25.  
  26.  
  27. $$ language plpgsql;
  28.  
  29.  
  30. -- использование
  31.  
  32. select * from stat();
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top