-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonoTypeMandatStrictementSuccessifAutreTypeSimultane.sql
46 lines (43 loc) · 1.58 KB
/
MonoTypeMandatStrictementSuccessifAutreTypeSimultane.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
CREATE OR REPLACE FUNCTION "BREF"."MonoTypeMandatStrictementSuccessifAutreTypeSimultane"(
)
RETURNS TABLE(idindividu character varying, idmandat integer)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
declare
rec record;
rec2 record;
id_indiv varchar;
datedebutmandat1 date;
datefinmandat2 date;
typemandat int;
BEGIN
id_indiv = '';
for rec in
select * from "BREF"."IndividuPlusDeuxMandatsDeuxMandatSuccessifsPlusInfos"
loop
if id_indiv != rec.idindividu then
id_indiv = rec.idindividu;
select "DateDebutMandat", "TypeDuMandat_IdTypeMandat" into datedebutmandat1, typemandat from "BREF"."Mandat" where "IdMandat" = rec.idmandat;
else
select "DateFinMandat" into datefinmandat2 from "BREF"."Mandat" where "IdMandat" = rec.idmandat;
for rec2 in
select "IdMandat", "DateDebutMandat", "DateFinMandat" from "BREF"."Mandat"
where "Elu_IdIndividu" = id_indiv
and "TypeDuMandat_IdTypeMandat" != typemandat
loop
if (rec2."DateDebutMandat" >= datedebutmandat1 and rec2."DateFinMandat" <= datefinmandat2)
or (rec2."DateDebutMandat" <= datedebutmandat1 and rec2."DateFinMandat" >= datedebutmandat1 and rec2."DateFinMandat" <= datefinmandat2)
or (rec2."DateDebutMandat" >= datedebutmandat1 and rec2."DateDebutMandat" <= datefinmandat2 and rec2."DateFinMandat" >= datefinmandat2)
or (rec2."DateDebutMandat" <= datedebutmandat1 and rec2."DateFinMandat" >= datefinmandat2) then
idindividu = id_indiv;
idmandat = rec2."IdMandat";
return next;
end if;
end loop;
end if;
end loop;
END;
$BODY$;