Author 
Topic 
AskSQLTeam
Ask SQLTeam Question
0 Posts 
Posted  20020201 : 07:45:33

Lou writes "I am trying to come up with a SQL statement that will figure the distance between one zip code and a table of another zip codes and return them sorted by distance.
The tables would be:
DISTANCE TABLE Zip Code, Latitude, Longitude
ZIP CODE TABLE Zip Code, Town Name
Another zip code would be provided as the starting point. Is this possible to do just in SQL?" 

robvolk
Most Valuable Yak
15732 Posts 
Posted  20020201 : 07:59:47

If you can use some basic trig functions like COS, SIN, and TAN to find the distances, then yes you can do this using on SQL. I'm not sure if the math is right, but the following example should give you an idea:
DECLARE @startzip char(5), @destzip char(5) SELECT @startzip='11701', @destzip='30097' SELECT Start.Latitude, Start.Longitude, Dest.Latitude, Dest.Longitude, TAN((Start.LatitudeDest.Latitude)/(Start.LongitudeDest.Longitude)) AS DistanceTAN FROM Distance Start, Distance Dest WHERE Start.ZipCode=@startzip AND Dest.ZipCode=@destzip
You can JOIN the table of zip codes to this instead of supply a single zip code as the destination. Can you provide some more detail? If you've got a formula for calculating distance we could plug it in to give you actual distances.



aiken
Aged Yak Warrior
525 Posts 
Posted  20020202 : 16:45:18

I have just this application running.
For performance, I precompute the distance between every zip code (!) and then throw out the ones that are over 2500 miles. It's a 38GB database, but that's really the only way you can do it if you want to be able to sort by "closest" (otherwise you have to compute distance for every possible combination in the query itself, which can take forever).
I keep the zip code data and stored procedures in a seperate database, as it never changes and I don't want to be backing it up all the time.
Cheers b



Arnold Fribble
Yakfinder General
1961 Posts 
Posted  20020203 : 05:28:53

Rob, what on earth are you doing with that tan?! You might want to sanity check this function with some known values: I haven't tested it extensively.
CREATE FUNCTION GCdist (@lat1 float, @lon1 float, @lat2 float, @lon2 float) RETURNS float AS BEGIN  Parameters in RADIANS, result in km  Haversine formula from http://www.census.gov/cgibin/geo/gisfaq?5.1 DECLARE @dlon float, @dlat float, @a float, @c float SET @dlon = @lon2  @lon1 SET @dlat = @lat2  @lat1 SET @a = POWER(SIN(@dlat / 2.0), 2.0) + COS(@lat1) * COS(@lat2) * POWER(SIN(@dlon / 2.0), 2.0) SET @c = 2.0 * ATN2(SQRT(@a), SQRT(1.0  @a)) RETURN 6367.0 * @c END
GO
CREATE FUNCTION dms2rad (@deg int, @min int, @sec float) RETURNS float AS BEGIN RETURN RADIANS(CONVERT(float, @deg) + CONVERT(float, @min)/60.0 + @sec/3600.0) END
Edited by  Arnold Fribble on 02/03/2002 06:10:18 


robvolk
Most Valuable Yak
15732 Posts 
Posted  20020203 : 08:06:08

quote: Rob, what on earth are you doing with that tan?!
Misusing it, most likely! It's been almost 20 years since I've had to use serious trigonometry. Give me a break!



Glockenspiel
Yak Posting Veteran
50 Posts 
Posted  20021015 : 18:19:20

Here's a function that is derived from the original one but further simplified. I verified that it will return the same results, although this one has been further modified to return the results in STATUTE MILES rather than kilometers (if you want it in km again then just replace the constant 69.041 with 6367; if you want it in nautical miles just use 60).
CREATE FUNCTION GCdist2 (@lat1 FLOAT, @lon1 FLOAT, @lat2 FLOAT, @lon2 FLOAT) RETURNS FLOAT AS BEGIN  Parameters in RADIANS, result in STATUTE MILES DECLARE @c FLOAT SET @c = ACOS(SIN(@lat1)*SIN(@lat2)+COS(@lat1)*COS(@lat2)*COS(@lon1@lon2)) RETURN 69.041 * @c END GO



Arnold Fribble
Yakfinder General
1961 Posts 
Posted  20021016 : 07:05:51

Hmm. When I posted that haversine formula, it was on the assumption that the argument in the referenced GIS faq was correct  that the cosine formula gives inaccurate results for small distances. What I'd failed to take into account is the difference it makes in going from single precision to double precision. The inaccuracy is still there, it's just that you need points a few inches apart to notice!
quote:
[...] return the results in STATUTE MILES rather than kilometers (if you want it in km again then just replace the constant 69.041 with 6367; if you want it in nautical miles just use 60).
By definition there are exactly 1.609344 km in a statute mile, and 1.852 km in a nautical mile. So the constants should be 3956 (rather than 69.041) and 3438 (rather than 60).
Edited by  Arnold Fribble on 10/16/2002 07:15:08 


liana
Starting Member
1 Post 
Posted  20060906 : 00:12:37

hi can anyone help me how to sort many pointer for example
1.1.1 1.1.2 1.1.3 and so on
have any idea? 


SwePeso
Patron Saint of Lost Yaks
30421 Posts 
Posted  20060906 : 02:50:38

This should be a separate topic!
If there are no more than three values in the string, use thisdeclare @test table (version varchar(100))
insert @test select '1.1.2' union all select '1.1.3' union all select '1.1.15' union all select '1.1.1'
select version from @test order by CAST(parsename(version, 3) AS INT), CAST(parsename(version, 2) AS INT), CAST(parsename(version, 1) AS INT)
Peter Larsson Helsingborg, Sweden 


Arnold Fribble
Yakfinder General
1961 Posts 
Posted  20060906 : 02:56:42

Blimey, it's a long time since I last saw this thread! 


