As far as I understand SQL, it's normal you repeat clauses. I'm pretty sure the optimizer takes care of that and they are not actually executed more than once.<br><br>Maybe indexes on the ST_Centroid functions would help?<br>
<br>Where do g.geom and f.geom come from?<br><br>Jo<br><br><br><br><div class="gmail_quote">2013/5/3 Alexandre Neto <span dir="ltr"><<a href="mailto:senhor.neto@gmail.com" target="_blank">senhor.neto@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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 style="white-space:pre-wrap">       </span>b.gid,</div>
<div><span style="white-space:pre-wrap">  </span>b.volume / t.total_volume as vol_per,</div><div><span style="white-space:pre-wrap">    </span>b.geom,</div><div><span style="white-space:pre-wrap">  </span>p.bgri11 as bgri11,</div>

<div>FROM</div><div><span style="white-space:pre-wrap">       </span>buildings as b,</div><div><span style="white-space:pre-wrap">  </span>stat_parcels as p,</div><div><span style="white-space:pre-wrap">      </span>(SELECT </div>
<div><span style="white-space:pre-wrap">          </span>sum(b.volume) as total_volume,</div><div><span style="white-space:pre-wrap">           </span>p.bgri11 as bgri11</div><div><span style="white-space:pre-wrap">       </span>FROM</div>
<div><span style="white-space:pre-wrap">          </span>buildings as b,</div><div><span style="white-space:pre-wrap">          </span>stat_parcels as p</div><div><span style="white-space:pre-wrap">       </span>WHERE</div>
<div><span style="white-space:pre-wrap">          </span>ST_CONTAINS(p.geom, St_centroid(b.geom))</div><div><span style="white-space:pre-wrap"> </span>GROUP BY</div><div><span style="white-space:pre-wrap">         </span>p.bgri11) as t</div>

<div>WHERE</div><div><span style="white-space:pre-wrap">      </span>ST_CONTAINS(g.geom, St_centroid(f.geom))</div><div><span style="white-space:pre-wrap"> </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>
<br>_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a><br>
<br></blockquote></div><br>