-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistoMonoMandatNuancePolitique.sql
48 lines (44 loc) · 1.27 KB
/
HistoMonoMandatNuancePolitique.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
47
48
CREATE OR REPLACE FUNCTION "BREF"."HistoMonoMandatNuancePolitique"(
)
RETURNS TABLE(nuancepolitique character, countindiv bigint, pourcentage numeric)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
declare
nbtotindiv int;
rec record;
BEGIN
select count(A."IdIndividu")
into nbtotindiv
from
(
select "IdIndividu"
from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
group by "IdIndividu"
having count("IdMandat") = 1
)A;
--raise notice '%', nbtotindiv;
for rec in
select "NuancePolitique", count("IdMandat") as countmandat
from
(
select "IdIndividu" from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
group by "IdIndividu"
having count("IdMandat") = 1
) A,
"BREF"."Mandat", "BREF"."NuancePolitique"
where "BREF"."Mandat"."Elu_IdIndividu" = A."IdIndividu"
and "BREF"."NuancePolitique"."IdNuancePolitique" = "BREF"."Mandat"."IdNuancePolitique"
group by "NuancePolitique"
loop
nuancepolitique := rec."NuancePolitique";
countindiv := rec.countmandat;
pourcentage := round(rec.countmandat*100/(nbtotindiv)::numeric(1000,1), 5);
return next;
end loop;
END;
$BODY$;