Can you please explain further why u used the EXPAND? Didn&#39;t much get what is its use? And is mmd a thing which has to do with PostGis ?<br><br><div><span class="gmail_quote">On 11/4/07, <b class="gmail_sendername">Paul Ramsey
</b> &lt;<a href="mailto:pramsey@refractions.net">pramsey@refractions.net</a>&gt; wrote:</span><blockquote class="gmail_quote" style="margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; margin-left: 0.80ex; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; padding-left: 1ex">
<br>Well, you have to build the cartesian product of every city<br>combination and then measure every distance in that virtual table, so<br>it&#39;s not going to scale well at all as the input table gets bigger.<br><br>However, if you know the &quot;maximum minimum distance&quot; (?mmd?) you can
<br>add a spatial constraint that should at least keep the calculations<br>in the O(n*log(n)) range... (you&#39;ll need a spatial index on the table<br>for best effect as the table gets larger)<br><br>SELECT DISTINCT ON (c1)
<br>&nbsp;&nbsp;&nbsp;&nbsp; c1.city_name AS &quot;c1&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp; c2.city_name AS &quot;c2&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp; distance(c1.the_geom, c2.the_geom),<br>&nbsp;&nbsp;&nbsp;&nbsp; makeline(c1.the_geom, c2.the_geom)<br>FROM<br>&nbsp;&nbsp;&nbsp;&nbsp; city c1<br>JOIN<br>&nbsp;&nbsp;&nbsp;&nbsp; city c2<br>&nbsp;&nbsp;&nbsp;&nbsp; ON (
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.city_name &lt;&gt; c2.city_name AND<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.the_geom &amp;&amp; ST_Expand(c2.the_geom, ?mmd?)<br>&nbsp;&nbsp;&nbsp;&nbsp; )<br>ORDER BY c1, distance ASC<br>;<br><br>Paul<br><br>PS - Nice query, BTW.<br><br>On 4-Nov-07, at 9:15 AM, Yancho wrote:
<br><br>&gt;<br>&gt; Just wanted to say that I managed to write this Query :<br>&gt;<br>&gt; SELECT DISTINCT ON (c1)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; c1.city_name AS &quot;c1&quot;,<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; c2.city_name AS &quot;c2&quot;,<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; distance(
c1.the_geom, c2.the_geom),<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; makeline(c1.the_geom, c2.the_geom)<br>&gt; FROM<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; city c1<br>&gt; JOIN<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; city c2<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; ON (<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.city_name &lt;&gt; c2.city_name<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; )
<br>&gt; ORDER BY c1, distance ASC<br>&gt; ;<br>&gt;<br>&gt; It works perfectly, however how much do you think it can scale ? On<br>&gt; 16 rows<br>&gt; it didnt take long, however or 28,000 rows? Will it use the O(n^2)<br>
&gt; scalability?<br>&gt;<br>&gt; Thanks<br>&gt;<br>&gt;<br>&gt; Yancho wrote:<br>&gt;&gt;<br>&gt;&gt; Hi,<br>&gt;&gt;<br>&gt;&gt; I am trying to make a query so it parses through all the 16 cities<br>&gt;&gt; i have<br>&gt;&gt; in
<br>&gt;&gt; a table called city, and for each city, picks the nearest city,<br>&gt;&gt; and gives<br>&gt;&gt; me<br>&gt;&gt; the distance between both cities.<br>&gt;&gt;<br>&gt;&gt; This is the query I made :<br>&gt;&gt;
<br>&gt;&gt; select<br>&gt;&gt; c.city_name, astext(c.the_geom), distance(c.the_geom, d.the_geom) AS<br>&gt;&gt; Distance, d.city_name, astext(d.the_geom)<br>&gt;&gt; from city c, city d<br>&gt;&gt; where<br>&gt;&gt; c.city_name
 = (<br>&gt;&gt; select c.city_name order by c.city_name ASC<br>&gt;&gt; )<br>&gt;&gt; and<br>&gt;&gt; d.city_name = (<br>&gt;&gt; select d.city_name order by d.city_name DESC<br>&gt;&gt; )<br>&gt;&gt; group by c.city_name
<br>&gt;&gt; order by Distance DESC<br>&gt;&gt; LIMIT 1;<br>&gt;&gt;<br>&gt;&gt; But I am getting this error : ERROR: column &quot;c.the_geom&quot; must<br>&gt;&gt; appear in<br>&gt;&gt; the<br>&gt;&gt; GROUP BY clause or be used in an aggregate function
<br>&gt;&gt;<br>&gt;&gt; I am seeing no reason why I should add c.the_geom, anyone can<br>&gt;&gt; enlighten me<br>&gt;&gt; more on why I should group by the_geom and after all if it does make<br>&gt;&gt; sense?<br>&gt;&gt;
<br>&gt;&gt; Thanks<br>&gt;&gt;<br>&gt;&gt; --<br>&gt;&gt; Matthew Pulis<br>&gt;&gt; <a href="http://www.solutions-lab.net">www.solutions-lab.net</a> // <a href="http://www.mepa-clan.info">www.mepa-clan.info</a><br>&gt;&gt;
<br>&gt;&gt; _______________________________________________<br>&gt;&gt; postgis-users mailing list<br>&gt;&gt; <a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>&gt;&gt; 
<a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>&gt;&gt;<br>&gt;&gt;<br>&gt;<br>&gt; --<br>&gt; View this message in context: <a href="http://www.nabble.com/Shortest-">
http://www.nabble.com/Shortest-</a><br>&gt; Distance-from-Every-Point-tf4743229.html#a13575499<br>&gt; Sent from the PostGIS - User mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<br>&gt;<br>&gt; _______________________________________________
<br>&gt; postgis-users mailing list<br>&gt; <a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>&gt; <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">
http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br><br>_______________________________________________<br>postgis-users mailing list<br><a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net
</a><br><a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br></blockquote></div><br><br clear="all"><br>-- <br>Matthew Pulis<br><a href="http://www.solutions-lab.net">
www.solutions-lab.net</a> // <a href="http://www.mepa-clan.info">www.mepa-clan.info</a>