Hello all,<br><div><div><div><br></div></div><div>My goal is to calculate for each building it's volume percentage relatively to the total buildings volume inside the statistical parcel he is in.</div></div><div><br></div>
<div>I have written the SQL code below (that works), but I think it might be improved, since I see repeated proceedings in it. </div><div><br></div><div>I would greatly appreciate if someone give me some advice about it.</div>
<div><br></div><div>Thanks,</div><div><br></div><div>Alexandre Neto</div><div><br></div><div>-----------</div><div><br></div><div><div>SELECT </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>b.gid,</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>b.volume / t.total_volume as vol_per,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>b.geom,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>p.bgri11 as bgri11,</div>
<div>FROM</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>buildings as b,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>stat_parcels as p,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(SELECT </div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>sum(b.volume) as total_volume,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>p.bgri11 as bgri11</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>FROM</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>buildings as b,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>stat_parcels as p</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>WHERE</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ST_CONTAINS(p.geom, St_centroid(b.geom))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>GROUP BY</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>p.bgri11) as t</div>
<div>WHERE</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ST_CONTAINS(g.geom, St_centroid(f.geom))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>AND p.bgri11 = t.bgri11;</div>
</div><div><br></div><div><br></div><div>My tables look like this:<div><br></div><div><div>CREATE TABLE buildings</div><div> gid serial NOT NULL,</div><div> volume double precision,</div><div> geom geometry(MultiPolygon,27493),</div>
<div> CONSTRAINT edificios_habitacao_pkey PRIMARY KEY (gid);</div><div><br></div><div><div>CREATE TABLE stat_parcels</div><div> gid serial NOT NULL,</div><div> bgri11 character varying(11),</div><div> geom geometry(MultiPolygon,27493),</div>
<div> CONSTRAINT bgri11_cascais_pkey PRIMARY KEY (gid)</div></div></div></div>