-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistoMonoMandatHF.sql
37 lines (36 loc) · 1.17 KB
/
HistoMonoMandatHF.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
CREATE OR REPLACE FUNCTION "BREF"."HistoMonoMandatHF"(
)
RETURNS TABLE(nbm bigint, pourcentagem numeric, nbf bigint, pourcentagef numeric)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
BEGIN
return query
select AA."NbM", AA."NbM"*100/(AA."NbM"+count(I2."IdIndividu"))::numeric(1000,1) as "pourcentageM",
count(I2."IdIndividu") as "NbF", count(I2."IdIndividu")*100/(AA."NbM"+count(I2."IdIndividu"))::numeric(1000,1) as "pourcentageF"
from
(
select count(A."IdIndividu") as "NbM" from
(
select "IdIndividu", count("IdMandat") from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
group by "IdIndividu"
having count("IdMandat") = 1
) A,
"BREF"."Individu" I1
where I1."IdIndividu" = A."IdIndividu"
and I1."Sexe" = 'M'
) AA,
(
select "IdIndividu", count("IdMandat") from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
group by "IdIndividu"
having count("IdMandat") = 1
) B, "BREF"."Individu" I2
where I2."IdIndividu" = B."IdIndividu"
and I2."Sexe" = 'F'
group by "NbM";
END;
$BODY$;