Skip to content

Commit

Permalink
actualisation code vues
Browse files Browse the repository at this point in the history
  • Loading branch information
lpojgc committed Feb 14, 2024
1 parent 532a4e2 commit 20b383f
Show file tree
Hide file tree
Showing 4 changed files with 545 additions and 499 deletions.
108 changes: 71 additions & 37 deletions config/mv_c_cor_vn_taxref.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,93 @@

CREATE MATERIALIZED VIEW taxonomie.mv_c_cor_vn_taxref
TABLESPACE pg_default
AS WITH prep AS (
AS WITH prep_vn AS (
SELECT DISTINCT sp.id AS vn_id,
tg.item ->> 'name'::text AS groupe_taxo_fr,
tg.item ->> 'latin_name'::text AS groupe_taxo_sci,
sp.item ->> 'french_name'::text AS french_name,
sp.item ->> 'latin_name'::text AS latin_name
FROM src_vn_json.species_json sp
LEFT JOIN src_vn_json.taxo_groups_json tg ON ((sp.item ->> 'id_taxo_group'::text)::integer) = tg.id AND sp.site::text = tg.site::text
GROUP BY sp.id, (tg.item ->> 'name'::text), (tg.item ->> 'latin_name'::text), (sp.item ->> 'french_name'::text), (sp.item ->> 'latin_name'::text)
GROUP BY sp.id, (tg.item ->> 'name'::text), (tg.item ->> 'latin_name'::text), (sp.item ->> 'french_name'::text), (sp.item ->> 'latin_name'::text)
), prep_tx AS (
SELECT tx.cd_nom,
tx.cd_ref,
tx.group1_inpn AS tx_group1_inpn,
tx.group2_inpn AS tx_group2_inpn,
tx.id_rang AS tx_id_rang,
tx.ordre AS tx_ordre,
tx.classe AS tx_classe,
tx.famille AS tx_famille,
tx.nom_vern AS tx_nom_fr,
tx.lb_nom AS tx_nom_sci
FROM taxonomie.taxref tx
WHERE tx.cd_nom = tx.cd_ref
), prep_synth AS (
SELECT prep_vn.vn_id,
string_agg(DISTINCT prep_vn.groupe_taxo_fr, ', '::text) AS groupe_taxo_fr,
string_agg(DISTINCT prep_vn.groupe_taxo_sci, ', '::text) AS groupe_taxo_sci,
COALESCE(string_agg(DISTINCT prep_vn.french_name, ','::text), string_agg(DISTINCT prep_tx.tx_nom_fr::text, ','::text)) AS vn_nom_fr,
COALESCE(string_agg(DISTINCT prep_vn.latin_name, ','::text), string_agg(DISTINCT prep_tx.tx_nom_sci::text, ', '::text)) AS vn_nom_sci,
prep_tx.cd_nom,
prep_tx.cd_ref,
string_agg(DISTINCT prep_tx.tx_group1_inpn::text, ', '::text) AS tx_group1_inpn,
string_agg(DISTINCT prep_tx.tx_group2_inpn::text, ', '::text) AS tx_group2_inpn,
string_agg(DISTINCT prep_tx.tx_id_rang::text, ', '::text) AS tx_id_rang,
string_agg(DISTINCT prep_tx.tx_ordre::text, ', '::text) AS tx_ordre,
string_agg(DISTINCT prep_tx.tx_classe::text, ', '::text) AS tx_classe,
string_agg(DISTINCT prep_tx.tx_famille::text, ', '::text) AS tx_famille,
string_agg(DISTINCT prep_tx.tx_nom_fr::text, ', '::text) AS tx_nom_fr,
string_agg(DISTINCT prep_tx.tx_nom_sci::text, ', '::text) AS tx_nom_sci
FROM prep_tx
LEFT JOIN taxonomie.cor_c_vn_taxref cor ON cor.cd_nom = prep_tx.cd_nom
LEFT JOIN prep_vn ON prep_vn.vn_id = cor.vn_id
GROUP BY prep_vn.vn_id, prep_tx.cd_nom, prep_tx.cd_ref
)
SELECT vn.vn_id,
vn.groupe_taxo_fr,
vn.groupe_taxo_sci,
vn.french_name AS vn_nom_fr,
vn.latin_name AS vn_nom_sci,
tx.cd_nom,
tx.cd_ref,
tx.group1_inpn AS tx_group1_inpn,
tx.group2_inpn AS tx_group2_inpn,
tx.classe AS tx_classe,
tx.famille AS tx_famille,
tx.nom_vern AS tx_nom_fr,
tx.lb_nom AS tx_nom_sci,
SELECT array_agg(DISTINCT ps1.vn_id) AS vn_id,
ps1.groupe_taxo_fr,
ps1.groupe_taxo_sci,
CASE
WHEN id_sp_vn.id_sp_source IS NOT NULL THEN true
ELSE false
END AS vn_utilisation
FROM prep vn
LEFT JOIN taxonomie.cor_c_vn_taxref corr ON vn.vn_id = corr.vn_id
LEFT JOIN taxonomie.taxref tx ON corr.cd_nom = tx.cd_nom
LEFT JOIN ( SELECT DISTINCT tcse.id_sp_source
FROM src_lpodatas.t_c_synthese_extended tcse
JOIN gn_synthese.synthese s ON s.id_synthese = tcse.id_synthese
WHERE (s.id_source IN ( SELECT ts.id_source
FROM gn_synthese.t_sources ts
WHERE ts.name_source::text = 'faune-aura'::text))) id_sp_vn ON id_sp_vn.id_sp_source = vn.vn_id
WHEN ps1.cd_nom = ps1.cd_ref THEN ps1.vn_nom_fr
WHEN ps2.cd_nom = ps1.cd_ref THEN ps2.vn_nom_fr
ELSE ps1.vn_nom_fr
END AS vn_nom_fr,
CASE
WHEN ps1.cd_nom = ps1.cd_ref THEN ps1.vn_nom_sci
WHEN ps2.cd_nom = ps1.cd_ref THEN ps2.vn_nom_sci
ELSE ps1.vn_nom_fr
END AS vn_nom_sci,
ps1.cd_ref AS cd_nom,
ps1.cd_ref,
ps1.tx_group1_inpn,
ps1.tx_group2_inpn,
ps1.tx_id_rang,
ps1.tx_ordre,
ps1.tx_classe,
ps1.tx_famille,
ps1.tx_nom_fr,
ps1.tx_nom_sci
FROM prep_synth ps1
LEFT JOIN prep_synth ps2 ON ps1.cd_ref = ps2.cd_ref
GROUP BY ps1.groupe_taxo_fr, ps1.groupe_taxo_sci, (
CASE
WHEN ps1.cd_nom = ps1.cd_ref THEN ps1.vn_nom_fr
WHEN ps2.cd_nom = ps1.cd_ref THEN ps2.vn_nom_fr
ELSE ps1.vn_nom_fr
END), (
CASE
WHEN ps1.cd_nom = ps1.cd_ref THEN ps1.vn_nom_sci
WHEN ps2.cd_nom = ps1.cd_ref THEN ps2.vn_nom_sci
ELSE ps1.vn_nom_fr
END), ps1.cd_ref, ps1.tx_group1_inpn, ps1.tx_group2_inpn, ps1.tx_id_rang, ps1.tx_ordre, ps1.tx_classe, ps1.tx_famille, ps1.tx_nom_fr, ps1.tx_nom_sci
WITH DATA;

-- View indexes:
CREATE INDEX mv_c_cor_vn_taxref_cd_nom_idx1 ON taxonomie.mv_c_cor_vn_taxref USING btree (cd_nom);
CREATE INDEX mv_c_cor_vn_taxref_cd_ref_idx1 ON taxonomie.mv_c_cor_vn_taxref USING btree (cd_ref);
CREATE INDEX mv_c_cor_vn_taxref_groupe_taxo_fr_idx1 ON taxonomie.mv_c_cor_vn_taxref USING btree (groupe_taxo_fr);
CREATE INDEX mv_c_cor_vn_taxref_tx_group2_inpn_idx1 ON taxonomie.mv_c_cor_vn_taxref USING btree (tx_group2_inpn);
CREATE INDEX mv_c_cor_vn_taxref_tx_nom_fr_idx1 ON taxonomie.mv_c_cor_vn_taxref USING btree (tx_nom_fr);
CREATE INDEX mv_c_cor_vn_taxref_tx_nom_sci_idx1 ON taxonomie.mv_c_cor_vn_taxref USING btree (tx_nom_sci);
CREATE INDEX mv_c_cor_vn_taxref_vn_nom_fr_idx1 ON taxonomie.mv_c_cor_vn_taxref USING btree (vn_nom_fr);
CREATE INDEX mv_c_cor_vn_taxref_vn_nom_sci_idx1 ON taxonomie.mv_c_cor_vn_taxref USING btree (vn_nom_sci);
CREATE INDEX mv_c_cor_vn_taxref_vn_utilisation_idx1 ON taxonomie.mv_c_cor_vn_taxref USING btree (vn_utilisation);


-- Permissions

ALTER TABLE taxonomie.mv_c_cor_vn_taxref OWNER TO dbadmin;
GRANT ALL ON TABLE taxonomie.mv_c_cor_vn_taxref TO postgres;
GRANT ALL ON TABLE taxonomie.mv_c_cor_vn_taxref TO dbadmin;
GRANT SELECT ON TABLE taxonomie.mv_c_cor_vn_taxref TO dt;
GRANT ALL ON TABLE taxonomie.mv_c_cor_vn_taxref TO advanced_user;
Loading

0 comments on commit 20b383f

Please sign in to comment.