-- View: mf.vu_histo_actes_par_site

-- DROP VIEW mf.vu_histo_actes_par_site;

CREATE OR REPLACE VIEW mf.vu_histo_actes_par_site AS 
 SELECT join3.code_site, join3.code_acte, join3.type_acte, join3.type_maitrise, histo_acte.date_acte, histo_acte.nom_notaire, histo_acte.prix_acquisition, histo_acte.origine_propriete, histo_acte.duree_conv, histo_acte.echeance_prevue
   FROM ( SELECT DISTINCT ON (histo_parcelle.code_site, join2.code_acte) histo_parcelle.code_site, join2.code_acte, join2.type_acte, join2.type_maitrise
           FROM ( SELECT join1.code_acte, join1.type_acte, type_maitrise.type_maitrise
                   FROM ( SELECT histo_acte.code_acte, type_acte.type_acte, histo_acte.ref_type_maitrise
                           FROM mf.histo_acte
                      LEFT JOIN mf.type_acte ON histo_acte.ref_type_acte = type_acte.id_type_acte) join1
              LEFT JOIN mf.type_maitrise ON join1.ref_type_maitrise = type_maitrise.id_type_maitrise) join2
      JOIN mf.histo_parcelle ON histo_parcelle.code_acte::text = join2.code_acte::text) join3
   JOIN mf.histo_acte ON histo_acte.code_acte::text = join3.code_acte::text;

ALTER TABLE mf.vu_histo_actes_par_site OWNER TO ludodo;
GRANT ALL ON TABLE mf.vu_histo_actes_par_site TO ludodo;
GRANT ALL ON TABLE mf.vu_histo_actes_par_site TO geomatique;
GRANT SELECT, REFERENCES, TRIGGER ON TABLE mf.vu_histo_actes_par_site TO public;
GRANT SELECT, REFERENCES, TRIGGER ON TABLE mf.vu_histo_actes_par_site TO cpnrc;
