/ db / test / sql /
/db/test/sql/groupinfants_day1
1 -- Copyright (C) 2013 The Meme Factory, Inc., http://www.meme.com/
2 --
3 -- This file is part of Gombe-MI.
4 --
5 -- Gombe-MI is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 3 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with Gombe-MI. If not, see <http://www.gnu.org/licenses/>.
17 --
18 -- Add the infants of the follows as groupmembers to the existing groups
19 -- (for all (9am to 5pm) intervals of follows 1-2, those on the first day).
20 --
21 -- All data appearing in this work are fictitious. Any resemblance to
22 -- actual data, living or archived, is purely coincidental.
23 --
24 -- Karl O. Pinc <kop@meme.com>
25 --
26
27 do $$
28 declare
29 this_time time;
30 this_infant varchar array;
31 a_infant varchar;
32 begin
33
34 -- Find the infants for each of the 4 follows.
35 for fid in 1 .. 2 loop
36 select followparts.chimpid
37 into strict a_infant
38 from followparts
39 join followroles on (followroles.role = followparts.role)
40 where followparts.followid = fid
41 and followroles.function = 'I';
42 this_infant[fid] := a_infant;
43 end loop;
44
45 -- The group composition is all taken at 5 minute intervals so use
46 -- that to find the groups.
47
48 this_time := '9:00';
49 while this_time <= '17:00' loop
50
51 for fid in 1 .. 2 loop
52 -- Give each of the groups an infant.
53 insert into groupmembers (gid, chimpid, uncertain)
54 select intervals.gid, this_infant[fid], 'f'
55 from intervals
56 where intervals.followid = fid
57 and intervals.time = this_time;
58 end loop;
59
60 this_time := this_time + '5 minute'::interval;
61 end loop;
62 end;
63 $$;
64