 
 -- Cutting
 
-CREATE TABLE IF NOT EXISTS model_draft."ego_grid_lv_cut"
+CREATE TABLE IF NOT EXISTS model_draft."ego_grid_lv_griddistrict"
 (
   id serial NOT NULL,
   geom geometry(Polygon,3035),

	FROM
		model_draft."ego_grid_mvlv_substation" AS onts 
		INNER JOIN 
		model_draft."ego_grid_lv_cut" AS regions ON onts.load_area_id = regions.load_area_id
		INNER JOIN
		mins ON mins.regions_id = regions.id
	WHERE  ST_DISTANCE(ST_CENTROID(regions.geom),onts.geom) = mins.distance
      ) AS t2
WHERE t1.id = t2.district_id;

UPDATE 	model_draft."ego_grid_lv_cut" AS t1
SET ont_id = merge_id
WHERE ont_count = 0;


INSERT INTO		model_draft."ego_grid_lv_griddistrictwithoutpop" (geom,load_area_id)

SELECT (ST_DUMP(ST_UNION(cut.geom))).geom::geometry(Polygon,3035), onts.load_area_id
FROM model_draft."ego_grid_lv_griddistrict" AS cut
	INNER JOIN model_draft."ego_grid_mvlv_substation" AS onts
ON cut.ont_id = onts.id
WHERE ont_id >= 0
