-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistoTypeMandatNuancePolitique.sql
156 lines (146 loc) · 6.58 KB
/
HistoTypeMandatNuancePolitique.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
CREATE OR REPLACE FUNCTION "BREF"."HistoTypeMandatNuancePolitique"(
)
RETURNS TABLE(nbmandat text, nuancepolitique character varying, countmandat integer, pourcentage numeric)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
declare
rec record;
nbtotmandat int;
nb_mandat text;
nuance_politique varchar;
count_mandat int;
pourcentage_n numeric;
BEGIN
select sum(A.countmandat)
into nbtotmandat
from
(
select "IdIndividu", "TypeMandat", count("IdMandat") as countmandat from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."IdTypeMandat" = "BREF"."Mandat"."TypeDuMandat_IdTypeMandat"
group by "IdIndividu", "TypeMandat"
having count("IdMandat") = 2
)A;
for rec in
select '2' as nb_mandat, "NuancePolitique" as nuance_politique, count("IdMandat") as count_mandat,
round(count("IdMandat")*100/(nbtotmandat)::numeric(1000,1), 5) as pourcentage_n
from
(
select "IdIndividu", "TypeMandat", count("IdMandat") as countmandat from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."IdTypeMandat" = "BREF"."Mandat"."TypeDuMandat_IdTypeMandat"
group by "IdIndividu", "TypeMandat"
having count("IdMandat") = 2
)A
join "BREF"."Mandat" on "BREF"."Mandat"."Elu_IdIndividu" = A."IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."TypeMandat" = A."TypeMandat" and "BREF"."Mandat"."TypeDuMandat_IdTypeMandat" = "BREF"."TypeMandat"."IdTypeMandat"
join "BREF"."NuancePolitique" on "BREF"."NuancePolitique"."IdNuancePolitique" = "BREF"."Mandat"."IdNuancePolitique"
group by "NuancePolitique"
loop
nbmandat := rec.nb_mandat;
nuancepolitique := rec.nuance_politique;
countmandat := rec.count_mandat;
pourcentage := rec.pourcentage_n;
return next;
end loop;
select sum(A.countmandat)
into nbtotmandat
from
(
select "IdIndividu", "TypeMandat", count("IdMandat") as countmandat from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."IdTypeMandat" = "BREF"."Mandat"."TypeDuMandat_IdTypeMandat"
group by "IdIndividu", "TypeMandat"
having count("IdMandat") = 3
)A;
for rec in
select '3' as nb_mandat, "NuancePolitique" as nuance_politique, count("IdMandat") as count_mandat,
round(count("IdMandat")*100/(nbtotmandat)::numeric(1000,1), 5) as pourcentage_n
from
(
select "IdIndividu", "TypeMandat", count("IdMandat") as countmandat from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."IdTypeMandat" = "BREF"."Mandat"."TypeDuMandat_IdTypeMandat"
group by "IdIndividu", "TypeMandat"
having count("IdMandat") = 3
)A
join "BREF"."Mandat" on "BREF"."Mandat"."Elu_IdIndividu" = A."IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."TypeMandat" = A."TypeMandat" and "BREF"."Mandat"."TypeDuMandat_IdTypeMandat" = "BREF"."TypeMandat"."IdTypeMandat"
join "BREF"."NuancePolitique" on "BREF"."NuancePolitique"."IdNuancePolitique" = "BREF"."Mandat"."IdNuancePolitique"
group by "NuancePolitique"
loop
nbmandat := rec.nb_mandat;
nuancepolitique := rec.nuance_politique;
countmandat := rec.count_mandat;
pourcentage := rec.pourcentage_n;
return next;
end loop;
select sum(A.countmandat)
into nbtotmandat
from
(
select "IdIndividu", "TypeMandat", count("IdMandat") as countmandat from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."IdTypeMandat" = "BREF"."Mandat"."TypeDuMandat_IdTypeMandat"
group by "IdIndividu", "TypeMandat"
having count("IdMandat") = 4
)A;
for rec in
select '4' as nb_mandat, "NuancePolitique" as nuance_politique, count("IdMandat") as count_mandat,
round(count("IdMandat")*100/(nbtotmandat)::numeric(1000,1), 5) as pourcentage_n
from
(
select "IdIndividu", "TypeMandat", count("IdMandat") as countmandat from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."IdTypeMandat" = "BREF"."Mandat"."TypeDuMandat_IdTypeMandat"
group by "IdIndividu", "TypeMandat"
having count("IdMandat") = 4
)A
join "BREF"."Mandat" on "BREF"."Mandat"."Elu_IdIndividu" = A."IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."TypeMandat" = A."TypeMandat" and "BREF"."Mandat"."TypeDuMandat_IdTypeMandat" = "BREF"."TypeMandat"."IdTypeMandat"
join "BREF"."NuancePolitique" on "BREF"."NuancePolitique"."IdNuancePolitique" = "BREF"."Mandat"."IdNuancePolitique"
group by "NuancePolitique"
loop
nbmandat := rec.nb_mandat;
nuancepolitique := rec.nuance_politique;
countmandat := rec.count_mandat;
pourcentage := rec.pourcentage_n;
return next;
end loop;
select sum(A.countmandat)
into nbtotmandat
from
(
select "IdIndividu", "TypeMandat", count("IdMandat") as countmandat from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."IdTypeMandat" = "BREF"."Mandat"."TypeDuMandat_IdTypeMandat"
group by "IdIndividu", "TypeMandat"
having count("IdMandat") > 4
)A;
for rec in
select '2' as nb_mandat, "NuancePolitique" as nuance_politique, count("IdMandat") as count_mandat,
round(count("IdMandat")*100/(nbtotmandat)::numeric(1000,1), 5) as pourcentage_n
from
(
select "IdIndividu", "TypeMandat", count("IdMandat") as countmandat from "BREF"."Mandat"
join "BREF"."Individu" on "BREF"."Individu"."IdIndividu" = "BREF"."Mandat"."Elu_IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."IdTypeMandat" = "BREF"."Mandat"."TypeDuMandat_IdTypeMandat"
group by "IdIndividu", "TypeMandat"
having count("IdMandat") > 4
)A
join "BREF"."Mandat" on "BREF"."Mandat"."Elu_IdIndividu" = A."IdIndividu"
join "BREF"."TypeMandat" on "BREF"."TypeMandat"."TypeMandat" = A."TypeMandat" and "BREF"."Mandat"."TypeDuMandat_IdTypeMandat" = "BREF"."TypeMandat"."IdTypeMandat"
join "BREF"."NuancePolitique" on "BREF"."NuancePolitique"."IdNuancePolitique" = "BREF"."Mandat"."IdNuancePolitique"
group by "NuancePolitique"
loop
nbmandat := rec.nb_mandat;
nuancepolitique := rec.nuance_politique;
countmandat := rec.count_mandat;
pourcentage := rec.pourcentage_n;
return next;
end loop;
END;
$BODY$;