Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 692239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:37:49+00:00 2026-05-14T02:37:49+00:00

I have a database full of two-dimensional data – points on a map. Each

  • 0

I have a database full of two-dimensional data – points on a map. Each record has a field of the geometry type. What I need to be able to do is pass a point to a stored procedure which returns the k nearest points (k would also be passed to the sproc, but that’s easy). I’ve found a query at http://blogs.msdn.com/isaac/archive/2008/10/23/nearest-neighbors.aspx which gets the single nearest neighbour, but I can’t figure how to extend it to find the k nearest neighbours.

This is the current query – T is the table, g is the geometry field, @x is the point to search around, Numbers is a table with integers 1 to n:

DECLARE @start FLOAT = 1000; 
WITH NearestPoints AS
(
     SELECT TOP(1) WITH TIES *,  T.g.STDistance(@x) AS dist
     FROM Numbers JOIN T WITH(INDEX(spatial_index)) 
     ON T.g.STDistance(@x) < @start*POWER(2,Numbers.n)
     ORDER BY n
)
SELECT TOP(1) * FROM NearestPoints
ORDER BY n, dist

The inner query selects the nearest non-empty region and the outer query then selects the top result from that region; the outer query can easily be changed to (e.g.) SELECT TOP(20), but if the nearest region only contains one result, you’re stuck with that.

I figure I probably need to recursively search for the first region containing k records, but without using a table variable (which would cause maintenance problems as you have to create the table structure and it’s liable to change – there’re lots of fields), I can’t see how.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:37:49+00:00Added an answer on May 14, 2026 at 2:37 am

    What happens if you remove TOP (1) WITH TIES from the inner query, and set the outer query to return the top k rows?

    I’d also be interested to know whether this amendment helps at all. It ought to be more efficient than using TOP:

    DECLARE @start FLOAT = 1000
            ,@k INT = 20
            ,@p FLOAT = 2;
    
    WITH NearestPoints AS
    (
         SELECT *
                ,T.g.STDistance(@x) AS dist
                ,ROW_NUMBER() OVER (ORDER BY T.g.STDistance(@x)) AS rn
         FROM Numbers 
         JOIN T WITH(INDEX(spatial_index)) 
         ON   T.g.STDistance(@x) <  @start*POWER(@p,Numbers.n)
         AND (Numbers.n - 1 = 0 
              OR T.g.STDistance(@x) >= @start*POWER(@p,Numbers.n - 1)
             )
    )
    SELECT * 
    FROM NearestPoints
    WHERE rn <= @k;
    

    NB – untested – I don’t have access to SQL 2008 here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database full of customer data. It's so big that it's really
I have a database table full of some really ugly and messy data. In
I have a customer that has set up a full backup of the database
I have a database full of small HTML documents and I need to programmatically
I have an sqlite database full of huge number of URLs and it's taking
I have a database with a table which is full of conditions and error
I have database with many tables. In the first table, I have a field
I have a database for a Train company. I want to combine two queries
Can you recommend a full-text search engine? (Preferably open source) I have a database
I have a database table in Sql Server 2008 R2 which contains data stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.