Projet

Général

Profil

INTER_CALENDRIER_VUE_26_02_2019.sql

requete creation vue matérialisée - Clément Leneveu, 27/03/2019 15:13

 
1
DROP VIEW public."INTER_CALENDRIER_VUE";
2

    
3
CREATE MATERIALIZED VIEW public."INTER_CALENDRIER_VUE" AS
4
 SELECT row_number() OVER () AS id,
5
    tab."idSeanceSonate",
6
    tab."idEvenementSonate",
7
    tab."idUtilisateurSonate",
8
    string_agg(DISTINCT tab."idSalle"::text, ', '::text) AS "idSalle",
9
    tab."idModule",
10
    tab."NomModule",
11
    tab."NomSeance",
12
    tab."DateDebut",
13
    tab."DateFin",
14
    string_agg(DISTINCT tab."NomProfesseur", ', '::text) AS "NomProfesseur",
15
    string_agg(DISTINCT tab."Salle"::text, ', '::text) AS "Salle",
16
    string_agg(DISTINCT tab."CodeSalle"::text, ', '::text) AS "CodeSalle",
17
    tab."Type",
18
    tab.nom,
19
    tab.prenom,
20
    tab.pseudocentrale,
21
    string_agg(DISTINCT tab."GroupeTD"::text, '|'::text) AS "GroupeTD"
22
   FROM (
23
     -- ON VA CHERCHER LES SEANCES POUR LES ETUDIANTS
24
   SELECT DISTINCT
25
     seance.id_seance AS "idSeanceSonate",
26
           0 AS "idEvenementSonate",
27
               CASE
28
                   WHEN usrprof.id_user IS NULL THEN 0::bigint
29
                   ELSE usrprof.id_user
30
               END AS "idUtilisateurSonate",
31
           salle.id_salle AS "idSalle",
32
           mo.id_module AS "idModule",
33
           mo.nom AS "NomModule",
34
           seance.nom AS "NomSeance",
35
           ((seance.dateseance || ' '::text) || seance.heuredeb::text) || ':00'::text AS "DateDebut",
36
           ((seance.dateseance || ' '::text) || seance.heurefin::text) || ':00'::text AS "DateFin",
37
           (usrprof.nom::text || ' '::text) || initcap(usrprof.prenom::text) AS "NomProfesseur",
38
           salle.nom AS "Salle",
39
           salle.code AS "CodeSalle",
40
           typseance.nom AS "Type",
41
           usrprof.nom,
42
           initcap(usrprof.prenom::text) AS prenom,
43
           usrprof.logindomaine AS pseudocentrale,
44
           gpe_td.nom AS "GroupeTD"
45
          FROM "ERP_Seance" seance
46
            LEFT JOIN "ERP_GroupeSeanceReelle" seance_relle ON seance.id_seance = seance_relle.id_seance
47
            LEFT JOIN "ERP_InscriptionInstanceModule" iim ON seance_relle.id_instancemodule = iim.id_instancemodule
48
            LEFT JOIN "ERP_InstanceModule" imo ON seance_relle.id_instancemodule = imo.id_instancemodule
49
            JOIN "ERP_AnneeScolaire" ann_scol ON imo.id_debutanneescolaire = ann_scol.id_anneedebut AND ann_scol.encours = TRUE
50
            LEFT JOIN "ERP_Module" mo ON imo.id_module = mo.id_module
51
            LEFT JOIN "ERP_Etudiant" etu ON iim.id_etudiant = etu.id_etudiant
52
            LEFT JOIN "ERP_User" usr ON usr.id_user = etu.id_user
53
            LEFT JOIN "ERP_ProfAnime" profanim ON profanim.id_seance = seance.id_seance
54
            LEFT JOIN "ERP_Professeur" prof ON prof.id_prof = profanim.id_prof
55
            LEFT JOIN "ERP_User" usrprof ON prof.id_user = usrprof.id_user
56
            LEFT JOIN "ERP_SalleSeance" salseance ON seance.id_seance = salseance.id_seance
57
            LEFT JOIN "ERP_Salle" salle ON salseance.id_salle = salle.id_salle
58
            LEFT JOIN "ERP_TypeSeance" typseance ON seance.id_typeseance = typseance.id_typeseance
59
            LEFT JOIN "ERP_InstanceGroupeTDModule" igpe_td_mo ON imo.id_instancemodule = igpe_td_mo.id_instancemodule AND iim.id_instancegroupetd = igpe_td_mo.id_instancegroupetd
60
            LEFT JOIN "ERP_InstanceGroupeTD" igpe_td ON igpe_td_mo.id_instancegroupetd = igpe_td.id_instancegroupetd
61
            LEFT JOIN "ERP_GroupeTd" gpe_td ON igpe_td.id_groupetd = gpe_td.id_groupetd
62
       UNION
63
      -- ON VA CHERCHER LES SEANCES POUR LES ENSEIGNANTS
64
        SELECT DISTINCT
65
        seance.id_seance AS "idSeanceSonate",
66
           0 AS "idEvenementSonate",
67
               CASE
68
                   WHEN usr.id_user IS NULL THEN 0::bigint
69
                   ELSE usr.id_user
70
               END AS "idUtilisateurSonate",
71
           salle.id_salle AS "idSalle",
72
           mo.id_module AS "idModule",
73
           mo.nom AS "NomModule",
74
           seance.nom AS "NomSeance",
75
           ((seance.dateseance || ' '::text) || seance.heuredeb::text) || ':00'::text AS "DateDebut",
76
           ((seance.dateseance || ' '::text) || seance.heurefin::text) || ':00'::text AS "DateFin",
77
           (usrprof.nom::text || ' '::text) || initcap(usrprof.prenom::text) AS "NomProfesseur",
78
           salle.nom AS "Salle",
79
           salle.code AS "CodeSalle",
80
           typseance.nom AS "Type",
81
           usr.nom,
82
           initcap(usr.prenom::text) AS prenom,
83
           usr.logindomaine AS pseudocentrale,
84
           gpe_td.nom AS "GroupeTD"
85
          FROM "ERP_Seance" seance
86
            LEFT JOIN "ERP_GroupeSeanceReelle" seance_relle ON seance.id_seance = seance_relle.id_seance
87
            LEFT JOIN "ERP_InscriptionInstanceModule" iim ON seance_relle.id_instancemodule = iim.id_instancemodule
88
            LEFT JOIN "ERP_InstanceModule" imo ON iim.id_instancemodule = imo.id_instancemodule
89
            JOIN "ERP_AnneeScolaire" ann_scol ON imo.id_debutanneescolaire = ann_scol.id_anneedebut AND ann_scol.encours = TRUE
90
            LEFT JOIN "ERP_Module" mo ON imo.id_module = mo.id_module
91
            LEFT JOIN "ERP_Etudiant" etu ON iim.id_etudiant = etu.id_etudiant
92
            LEFT JOIN "ERP_User" usr ON usr.id_user = etu.id_user
93
            LEFT JOIN "ERP_ProfAnime" profanim ON profanim.id_seance = seance.id_seance
94
            LEFT JOIN "ERP_Professeur" prof ON prof.id_prof = profanim.id_prof
95
            LEFT JOIN "ERP_User" usrprof ON prof.id_user = usrprof.id_user
96
            LEFT JOIN "ERP_SalleSeance" salseance ON seance.id_seance = salseance.id_seance
97
            LEFT JOIN "ERP_Salle" salle ON salseance.id_salle = salle.id_salle
98
            LEFT JOIN "ERP_TypeSeance" typseance ON seance.id_typeseance = typseance.id_typeseance
99
            LEFT JOIN "ERP_InstanceGroupeTDModule" igpe_td_mo ON imo.id_instancemodule = igpe_td_mo.id_instancemodule AND iim.id_instancegroupetd = igpe_td_mo.id_instancegroupetd
100
            LEFT JOIN "ERP_InstanceGroupeTD" igpe_td ON igpe_td_mo.id_instancegroupetd = igpe_td.id_instancegroupetd
101
            LEFT JOIN "ERP_GroupeTd" gpe_td ON igpe_td.id_groupetd = gpe_td.id_groupetd
102
           WHERE igpe_td.id_instancegroupetd = seance_relle.id_instancegroupetd
103
       UNION
104
       -- ON VA CHERCHER LES SEANCES POUR LES GROUPES DE TD COMPLEMENTAIRES
105
        SELECT DISTINCT seance.id_seance AS "idSeanceSonate",
106
           0 AS "idEvenementSonate",
107
               CASE
108
                   WHEN usr.id_user IS NULL THEN 0::bigint
109
                   ELSE usr.id_user
110
               END AS "idUtilisateurSonate",
111
           salle.id_salle AS "idSalle",
112
           mo.id_module AS "idModule",
113
           mo.nom AS "NomModule",
114
           seance.nom AS "NomSeance",
115
           ((seance.dateseance || ' '::text) || seance.heuredeb::text) || ':00'::text AS "DateDebut",
116
           ((seance.dateseance || ' '::text) || seance.heurefin::text) || ':00'::text AS "DateFin",
117
           (usrprof.nom::text || ' '::text) || initcap(usrprof.prenom::text) AS "NomProfesseur",
118
           salle.nom AS "Salle",
119
           salle.code AS "CodeSalle",
120
           typseance.nom AS "Type",
121
           usr.nom,
122
           initcap(usr.prenom::text) AS prenom,
123
           usr.logindomaine AS pseudocentrale,
124
           gpe_td.nom AS "GroupeTD"
125
          FROM "ERP_Seance" seance
126
            LEFT JOIN "ERP_GroupeSeanceReelle" seance_relle ON seance.id_seance = seance_relle.id_seance
127
            LEFT JOIN "ERP_InscriptionInstanceModule" iim ON seance_relle.id_instancemodule = iim.id_instancemodule
128
            JOIN "ERP_GPCOM_IIM" gp_comp ON iim.id_inscriptioninstancemodule = gp_comp.id_inscriptioninstancemodule
129
            LEFT JOIN "ERP_InstanceModule" imo ON iim.id_instancemodule = imo.id_instancemodule
130
            JOIN "ERP_AnneeScolaire" ann_scol ON imo.id_debutanneescolaire = ann_scol.id_anneedebut AND ann_scol.encours = TRUE
131
            LEFT JOIN "ERP_Module" mo ON imo.id_module = mo.id_module
132
            LEFT JOIN "ERP_Etudiant" etu ON iim.id_etudiant = etu.id_etudiant
133
            LEFT JOIN "ERP_User" usr ON usr.id_user = etu.id_user
134
            LEFT JOIN "ERP_ProfAnime" profanim ON profanim.id_seance = seance.id_seance
135
            LEFT JOIN "ERP_Professeur" prof ON prof.id_prof = profanim.id_prof
136
            LEFT JOIN "ERP_User" usrprof ON prof.id_user = usrprof.id_user
137
            LEFT JOIN "ERP_SalleSeance" salseance ON seance.id_seance = salseance.id_seance
138
            LEFT JOIN "ERP_Salle" salle ON salseance.id_salle = salle.id_salle
139
            LEFT JOIN "ERP_TypeSeance" typseance ON seance.id_typeseance = typseance.id_typeseance
140
            LEFT JOIN "ERP_InstanceGroupeTDModule" igpe_td_mo ON imo.id_instancemodule = igpe_td_mo.id_instancemodule AND gp_comp.id_instancegroupetd = igpe_td_mo.id_instancegroupetd
141
            LEFT JOIN "ERP_InstanceGroupeTD" igpe_td ON igpe_td_mo.id_instancegroupetd = igpe_td.id_instancegroupetd
142
            LEFT JOIN "ERP_GroupeTd" gpe_td ON igpe_td.id_groupetd = gpe_td.id_groupetd
143
          WHERE igpe_td.id_instancegroupetd = seance_relle.id_instancegroupetd
144
       UNION
145
       -- ON VA CHERCHER LES SEANCES EVALUATIONS (EXAMENS PLANIFIES)
146
        SELECT DISTINCT seance.id_seanceevaluation AS "idSeanceSonate",
147
           0 AS "idEvenementSonate",
148
               CASE
149
                   WHEN usr.id_user IS NULL THEN 0::bigint
150
                   ELSE usr.id_user
151
               END AS "idUtilisateurSonate",
152
           0 AS "idSalle",
153
           mo.id_module AS "idModule",
154
           mo.nom AS "NomModule",
155
           seance.nom AS "NomSeance",
156
           ((seance.dateseance || ' '::text) || seance.heuredeb::text) || ':00'::text AS "DateDebut",
157
           ((seance.dateseance || ' '::text) || seance.heurefin::text) || ':00'::text AS "DateFin",
158
           ''::text AS "NomProfesseur",
159
           salle.salles AS "Salle",
160
           ''::text AS "CodeSalle",
161
           typseance.nom AS "Type",
162
           usr.nom,
163
           usr.prenom,
164
           usr.logindomaine AS pseudocentrale,
165
           gpe_td.nom AS "GroupeTD"
166
          FROM "ERP_SeanceEvaluation" seance
167
            LEFT JOIN "ERP_SeanceEvaluationConcat" salle ON salle.id_seanceevaluation = seance.id_seanceevaluation
168
            LEFT JOIN "ERP_GroupeSeanceEvaluationReelle" s_grpe_eval_re ON seance.id_seanceevaluation = s_grpe_eval_re.id_seanceevaluation
169
            LEFT JOIN "ERP_InstanceModule" imo ON imo.id_instancemodule = s_grpe_eval_re.id_instancemodule
170
            JOIN "ERP_AnneeScolaire" ann_scol ON imo.id_debutanneescolaire = ann_scol.id_anneedebut AND ann_scol.encours = TRUE
171
            LEFT JOIN "ERP_Module" mo ON imo.id_module = mo.id_module
172
            LEFT JOIN "ERP_InscriptionInstanceModule" iim ON imo.id_instancemodule = iim.id_instancemodule
173
            LEFT JOIN "ERP_Etudiant" etu ON iim.id_etudiant = etu.id_etudiant
174
            LEFT JOIN "ERP_User" usr ON usr.id_user = etu.id_user
175
            LEFT JOIN "ERP_TypeSeance" typseance ON seance.id_typeseance = typseance.id_typeseance
176
            LEFT JOIN "ERP_InstanceGroupeTDModule" igpe_td_mo ON imo.id_instancemodule = igpe_td_mo.id_instancemodule AND iim.id_instancegroupetd = igpe_td_mo.id_instancegroupetd
177
            LEFT JOIN "ERP_InstanceGroupeTD" igpe_td ON igpe_td_mo.id_instancegroupetd = igpe_td.id_instancegroupetd
178
            LEFT JOIN "ERP_GroupeTd" gpe_td ON igpe_td.id_groupetd = gpe_td.id_groupetd
179
       UNION
180
       -- ON VA CHERCHER LES EVENEMENTS PLANIFIES
181
        SELECT DISTINCT 0 AS "idSeanceSonate",
182
           evtsalle.id_evenement AS "idEvenementSonate",
183
               CASE
184
                   WHEN usr_inv.id_user IS NOT NULL THEN usr_inv.id_user
185
                   ELSE 0::bigint
186
               END AS "idUtilisateurSonate",
187
           salle.id_salle AS "idSalle",
188
           0 AS "idModule",
189
           evt.nom AS "NomModule",
190
           evt.nom AS "NomSeance",
191
           ((evt.datedebutevenement || ' '::text) || evt.heuredebut::text) || ':00'::text AS "DateDebut",
192
           ((evt.datefinevenement || ' '::text) || evt.heurefin::text) || ':00'::text AS "DateFin",
193
           ''::text AS "NomProfesseur",
194
           salle.nom AS "Salle",
195
           salle.code AS "CodeSalle",
196
           evttype.nom AS "Type",
197
               CASE
198
                   WHEN usr_inv.id_user IS NOT NULL THEN usr_inv.nom
199
                   ELSE ''::character varying
200
               END AS nom,
201
               CASE
202
                   WHEN usr_inv.id_user IS NOT NULL THEN usr_inv.prenom
203
                   ELSE ''::character varying
204
               END AS prenom,
205
           ''::text AS pseudocentrale,
206
           ''::text AS "GroupeTD"
207
          FROM "ERP_Evenement" evt
208
            LEFT JOIN "ERP_SalleEvenement" evtsalle ON evtsalle.id_evenement = evt.id_evenement
209
            LEFT JOIN "ERP_TypeEvenement" evttype ON evttype.id_typeevenement = evt.id_typeevenement
210
            LEFT JOIN "ERP_Salle" salle ON evtsalle.id_salle = salle.id_salle
211
            LEFT JOIN "ERP_InviteEvenement" inv ON inv.id_evenement = evt.id_evenement
212
            LEFT JOIN "ERP_User" usr_inv ON inv.id_user = usr_inv.id_user
213
         WHERE (( SELECT "ERP_AnneeScolaire".datedebut
214
                  FROM "ERP_AnneeScolaire"
215
                 WHERE "ERP_AnneeScolaire".encours = true)) <= evt.datedebutevenement AND (( SELECT "ERP_AnneeScolaire".datefin
216
                  FROM "ERP_AnneeScolaire"
217
                 WHERE "ERP_AnneeScolaire".encours = true)) >= evt.datedebutevenement
218
       ) tab
219
  GROUP BY tab."idSeanceSonate", tab."idEvenementSonate", tab."idUtilisateurSonate", tab."NomSeance", tab."idModule", tab."NomModule", tab.pseudocentrale, tab."DateDebut", tab."DateFin", tab."Type", tab.nom, tab.prenom;
220

    
221
  ALTER TABLE "INTER_CALENDRIER_VUE"
222
    OWNER TO cri;
223

    
224
  GRANT SELECT ON "INTER_CALENDRIER_VUE" TO edt;
225

    
226
REFRESH MATERIALIZED VIEW "INTER_CALENDRIER_VUE";