-- View: mf.vu_actes_par_site

-- DROP VIEW mf.vu_actes_par_site;

CREATE OR REPLACE VIEW mf.vu_actes_par_site AS 

SELECT	join3.*,
		mf.acte.date_acte,
		mf.acte.nom_notaire,
		mf.acte.prix_acquisition,
		mf.acte.origine_propriete,
		mf.acte.duree_conv,
		mf.acte.echeance_prevue,
		mf.acte.ref_code_site
		
FROM (	SELECT DISTINCT ON (mf.parcelle.code_site, join2.code_acte)
						mf.parcelle.code_site,
						join2.code_acte,
						join2.type_acte,
						join2.type_maitrise
		FROM	(SELECT	join1.code_acte,
						join1.type_acte,
						mf.type_maitrise.type_maitrise
				FROM	(SELECT mf.acte.code_acte,
							mf.type_acte.type_acte,
							mf.acte.ref_type_maitrise
						FROM mf.acte
						LEFT JOIN mf.type_acte ON mf.acte.ref_type_acte=mf.type_acte.id_type_acte) AS join1
				LEFT JOIN mf.type_maitrise ON join1.ref_type_maitrise=mf.type_maitrise.id_type_maitrise) AS join2
		LEFT JOIN mf.parcelle ON mf.parcelle.code_acte::text = join2.code_acte::text) AS join3
JOIN mf.acte ON mf.acte.code_acte=join3.code_acte;

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

