sql-server - SQL Server 查询最近邻 DISTINCT TOP N ORDER BY 距离的空间数据

标签 sql-server spatial nearest-neighbor

我想知道是否有办法从空间数据列中查询最近的 N 个邻居,丢弃重复项。

例如,我的查询如下所示:

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

它非常有效,但我的结果是:
Distance
3906,81969203873
3906,81969203873
5321,62614141754
5756,28719382942

我试图放置一个 DISTINCT 子句,但它没有使用空间索引。

有任何想法吗?

谢谢

最佳答案

根据 Nearest Neighbor Query and Spatial Indexes 上的 MSDN 描述,您需要使用 STDistance()ORDER BY条款。

  1. The first expression in the ORDER BY clause must use the STDistance() method.

  2. Sort order for the first STDistance() expression in the ORDER BY clause must be ASC.


您可能需要使用 GROUP BYCTEsubquery以避免任何潜在的重复。

关于sql-server - SQL Server 查询最近邻 DISTINCT TOP N ORDER BY 距离的空间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42694366/

相关文章:

sql-server - 如何在没有 SQL Server 备份文件的情况下修复还原数据库?

MySQL GIS/空间扩展 - 融化我的大脑

sql - Microsoft SQL 浏览器客户端

sql - 如何为多行选择 NEWID()

spatial - 分析代码的空间和时间局部性

r - 如何估算空间权重矩阵的缺失邻居(皇后邻接)

machine-learning - WEKA kNN 和动态时间规整的截止值

r - R 中 KKNN 包中的最近邻在使用整个数据集时给出垃圾索引值

arrays - 在格上有效地计算邻居的功能

c# - 使用 Visual Studio 2017 在 SQL 数据库中插入数据