473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Query to list near neighbours

I am trying to write a query in Access (or SQL) that works on a table
like this:

Location Gridreference
Ben Nevis NQ1234512345
Ben Doon NQ1230012300
and so on for several thousand records.

I have a function that calculates the distance between two points.
What I want to do is write a query that will list for each point in
the database, which other points are within 500 metres.

I've managed to get the number of points within 500 metres and I can
list out the first point in the database within 500 metres, but I have
not managed a full list.

Any ideas?

Max
Nov 13 '05 #1
1 1386
Anne,

Thanks for the advice - and the clear explanation! I will try it out
this weekend - hope the size of the intermediate table doesn't scare
my old PC.

Thanks again,

Max

"Anne Nolan" <an***************@AOL.COM> wrote in message news:<2i************@uni-berlin.de>...
One approach:

Create a query (call it qryAllPairs, e.g.) that lists all pairs of points,
and the distance between them. This query will have your Locations table in
the query twice, with NO join between the two (this is called a "Cartesian
Product"):

SELECT Locations.Location AS FirstPoint, Locations_1.Location AS
SecondPoint, DistanceCalc(Locations.GridReference,
Locations_1.GridReference) AS Distance
FROM Locations, Locations AS Locations_1;

Note that each pair of points will appear twice, and each point will also be
paired with itself once. I've assumed a table name of "Locations" and a
function name of DistanceCalc.. change to whatever you've named yours.

SELECT FirstPoint, SecondPoint, Distance
FROM qryAllPairs
WHERE (FirstPoint < SecondPoint AND Distance < 500);

Then you can use this query as the basis for a 2nd query, which just gets
the point pairs where the Distance is less than 500.

The duplicate pairings are elimnated by including "WHERE FirstPoint <
SecondPoint ". This will also eliminate the records that pair a location
with itself.

If your distance calculation takes a long time, you may want to eliminate
the duplicate point pairs in the first query, so that you cut the number of
calls to the function in half.

Hope this helps,

Anne Nolan

"maxmarengo" <ma********@yahoo.co.uk> wrote in message
news:e5**************************@posting.google.c om...
I am trying to write a query in Access (or SQL) that works on a table
like this:

Location Gridreference
Ben Nevis NQ1234512345
Ben Doon NQ1230012300
and so on for several thousand records.

I have a function that calculates the distance between two points.
What I want to do is write a query that will list for each point in
the database, which other points are within 500 metres.

I've managed to get the number of points within 500 metres and I can
list out the first point in the database within 500 metres, but I have
not managed a full list.

Any ideas?

Max

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: vbnetrookie | last post by:
I have a table name in SQL Server 2000 that has a space in it ex: aim international I had trouble just in the query analyzer with this..I had to place the name in brackets for it to work. But...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
21
by: CSN | last post by:
I have a pretty simple select query that joins a table (p) with 125K rows with another table (pc) with almost one million rows: select p.* from product_categories pc inner join products p on...
1
by: Crash | last post by:
Hi, ..NET v1.x SP1 VS 2003 SQL Server 2000 SP3 Server 2000, XP, Server 2003 I would like to programmatically execute {possibly many} SQL Server batch scripts. Aka I have many scripts that...
6
by: windandwaves | last post by:
Hi Folk I have a query: SELECT COUNT( `SIS`.`ID` ) c, D FROM `SIS` , `SID` WHERE `SID_ID` = `SID`.`ID` AND `BRO` <> "bot" GROUP BY SID.ID
7
by: Frankie | last post by:
I'm trying to run the following search query: $query = sprintf ("SELECT itemNumber, thumbnailURL, title, description, price FROM apparel,hats WHERE apparel.title OR apparel.description OR...
6
by: gerbski | last post by:
Hi all, I am relatively new to ADO, but up to now I got things working the way I wanted. But now I've run into somethng really annoying. I am working in MS Access. I am using an Access...
22
by: Ubi | last post by:
I am looking for a list of countries and nationalities, against the top level domain. es: ..ar ==Argentina ==Argentinian ..al ==Albania ==Albanian etc. etc.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.