SDO_NN the second nearest neighbor

oracle-11gspatial

Oracle SQL geometry allows me to find the nearest neighbor using

SDO_NN(neighbors.GEOLOC,p.GEOLOC,'SDO_NUM_RES=1 UNIT = MILE',1) = 'TRUE')

How can I get the select the second nearest point?

Best Answer

From the docs

sdo_num_res Specifies the number of results (nearest neighbors) to be returned. If neither sdo_batch_size nor sdo_num_res is specified, this is equivalent to specifying sdo_batch_size=0. See the Usage Notes and Examples for more information. Data type is NUMBER. For example: 'sdo_num_res=5'

So just set sdo_num_res to whatever number of results you want returned.