Changes between Initial Version and Version 1 of UsersWikiExamplesAntiNear


Ignore:
Timestamp:
Apr 14, 2009, 8:52:26 AM (15 years ago)
Author:
pierre
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiExamplesAntiNear

    v1 v1  
     1= Examples Anti Near =
     2
     3
     4== Anti Near Neighbor Search - Aka the Non Join Join ==
     5
     6Problem: Find all locations who are more than 3000 meters away from their closest hospital.
     7
     8Solution: This example uses the trick that the primary key or non-nullable field of the right table in a left join will return NULL only in the situation where there is no match to the left table.  So here we first ask the question what hospitals are within 3000 meters from each location and then we throw out all the records that have hospitals within that distance.
     9
     10SELECT l.location_name
     11FROM locations As l
     12LEFT JOIN hospitals h ON ST_DWithin(l.the_geom, h.the_geom,3000)
     13WHERE h.hospital_id IS NULL;
     14
     15
     16Another way of solving the same problem:
     17
     18SELECT l.location_name
     19FROM locations As l
     20INNER JOIN hospitals h ON not ST_DWithin(l.the_geom, h.the_geom,3000);
     21
     22We just switch the result of the boolean question ST_DWithin and get the ones not matching.
     23
     24or maybe :
     25
     26SELECT l.location_name
     27FROM locations As l, hospitals h
     28WHERE not ST_DWithin(l.the_geom, h.the_geom,3000);