SQL Server Query Spatial Data for Nearest Neighbor DISTINCT TOP N ORDER BY Distance

I would like to know if there is a way to query the nearest N neighbors from a spatial data column by dropping duplicates.

For example, my query looks like this:

SELECT TOP (@N) Point.STDistance(@Point) AS Distance
FROM MyTable
WHERE Point.STDistance(@Point) IS NOT NULL --For Spatial Index usage
ORDER BY Distance;

This is very effective, but my results are:

Distance
3906,81969203873
3906,81969203873
5321,62614141754
5756,28719382942

I tried to put a DISTINCT clause, but then it does not use the spatial index.

Any ideas?

thank

+4
source share
2 answers

According to the MSDN description on the nearest neighboring query and spatial indexes you need to use STDistance()in ORDER BY.

  1. ORDER BY STDistance().

  2. STDistance() ORDER BY ASC.

GROUP BY CTE subquery, .

+1

- . , . , , . .

-2

Source: https://habr.com/ru/post/1671879/


All Articles