SELECT DISTINCT ON (ensembles_pt.codensemb)
	ensembles_pt.gid,
	ensembles_pt.codensemb,
	ensembles_pt.nom,
	ensembles_pt.geom
FROM spatial.ensembles_pt
JOIN (SELECT DISTINCT ON (sites_emprise.gid)
			sites_emprise.codsit
			FROM spatial.lignes_electriques_merged, spatial.sites_emprise
			WHERE st_DWithin(lignes_electriques_merged.geom, sites_emprise.geom, 1000)) ssreq
ON ensembles_pt.codens_txt=substring(ssreq.codsit, 1, 5)
ORDER BY ensembles_pt.codensemb;