The purpose of this puzzle is to determine which U.S. State capitals are within 100 miles of another U.S. State capital. The following script will create a StateCapitals table, and a calculate distance function.
Here are the rules...
1. Code should work on SQL 2000.
2. 1 SQL query, no temp tables, no table variables.
3. Ordered by state first, then by distance.
4. Distance rounded to the nearest 5 miles.
Nobody post code for a couple days (Let's say Thursday).
The output should look like...
[tt]
FromCapital ToCapital Distance
--------------------- ------------------------- --------
Hartford, Connecticut Providence, Rhode Island 65
Hartford, Connecticut Albany, New York 90
Hartford, Connecticut Boston, Massachusetts 95
[/tt]
The data...
The calculate Distance function...
-George
Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
Here are the rules...
1. Code should work on SQL 2000.
2. 1 SQL query, no temp tables, no table variables.
3. Ordered by state first, then by distance.
4. Distance rounded to the nearest 5 miles.
Nobody post code for a couple days (Let's say Thursday).
The output should look like...
[tt]
FromCapital ToCapital Distance
--------------------- ------------------------- --------
Hartford, Connecticut Providence, Rhode Island 65
Hartford, Connecticut Albany, New York 90
Hartford, Connecticut Boston, Massachusetts 95
[/tt]
The data...
Code:
Create Table StateCapitals(State VarChar(20), Capital VarChar(30), Latitude Decimal(8,5), Longitude Decimal(8,5))
Insert Into StateCapitals(State, Capital, Latitude, Longitude)
Select 'Alabama', 'Montgomery',32.35699,-86.25782 Union All
Select 'Alaska', 'Juneau',58.37356,-134.52395 Union All
Select 'Arizona', 'Phoenix',33.70397,-112.35184 Union All
Select 'Arkansas', 'Little Rock',34.75600,-92.28483 Union All
Select 'California', 'Sacramento',38.38046,-121.55541 Union All
Select 'Colorado', 'Denver',39.72630,-104.85681 Union All
Select 'Connecticut', 'Hartford',41.78007,-72.67710 Union All
Select 'Delaware', 'Dover',39.15638,-75.49549 Union All
Select 'Florida', 'Tallahassee',30.41851,-84.20338 Union All
Select 'Georgia', 'Atlanta',33.89125,-84.07456 Union All
Select 'Hawaii', 'Honolulu',24.85983,-168.02182 Union All
Select 'Idaho', 'Boise',43.60377,-116.27292 Union All
Select 'Illinois', 'Springfield',39.82084,-89.59898 Union All
Select 'Indiana', 'Indianapolis',39.77509,-86.13216 Union All
Select 'Iowa', 'Des Moines',41.67269,-93.57217 Union All
Select 'Kansas', 'Topeka',38.98808,-95.78066 Union All
Select 'Kentucky', 'Frankfort',38.22812,-84.86971 Union All
Select 'Louisiana', 'Baton Rouge',30.44924,-91.18561 Union All
Select 'Maine', 'Augusta',44.35164,-69.80377 Union All
Select 'Maryland', 'Annapolis',38.96731,-76.61014 Union All
Select 'Massachusetts', 'Boston',42.37057,-71.02696 Union All
Select 'Michigan', 'Lansing',42.59918,-84.37197 Union All
Select 'Minnesota', 'St. Paul',44.94446,-93.09311 Union All
Select 'Mississippi', 'Jackson',32.29110,-90.19269 Union All
Select 'Missouri', 'Jefferson City',38.49403,-92.16519 Union All
Select 'Montana', 'Helena',46.70934,-112.19762 Union All
Select 'Nebraska', 'Lincoln',40.86514,-96.82313 Union All
Select 'Nevada', 'Carson City',39.15449,-119.73074 Union All
Select 'New Hampshire', 'Concord',43.23031,-71.53610 Union All
Select 'New Jersey', 'Trenton',40.28053,-74.71202 Union All
Select 'New Mexico', 'Santa Fe',35.68695,-105.93727 Union All
Select 'New York', 'Albany',42.61485,-73.97081 Union All
Select 'North Carolina', 'Raleigh',35.77376,-78.60418 Union All
Select 'North Dakota', 'Bismarck',46.98121,-100.50272 Union All
Select 'Ohio', 'Columbus',40.04011,-82.89722 Union All
Select 'Oklahoma', 'Oklahoma City',35.49161,-97.56282 Union All
Select 'Oregon', 'Salem',44.90472,-122.92172 Union All
Select 'Pennsylvania', 'Harrisburg',40.26459,-76.86964 Union All
Select 'Rhode Island', 'Providence',41.82275,-71.41445 Union All
Select 'South Carolina', 'Columbia',33.98745,-81.02486 Union All
Select 'South Dakota', 'Pierre',44.33407,-100.09420 Union All
Select 'Tennessee', 'Nashville',36.16569,-86.77810 Union All
Select 'Texas', 'Austin',30.32637,-97.77126 Union All
Select 'Utah', 'Salt Lake City',40.75610,-111.90072 Union All
Select 'Vermont', 'Montpelier',44.19906,-72.55964 Union All
Select 'Virginia', 'Richmond',37.52425,-77.49316 Union All
Select 'Washington', 'Olympia',46.98477,-122.86908 Union All
Select 'West Virginia', 'Charleston',38.32895,-81.60509 Union All
Select 'Wisconsin', 'Madison',43.06956,-89.42386 Union All
Select 'Wyoming', 'Cheyenne',41.25173,-104.56264
The calculate Distance function...
Code:
Create Function fnCalculateDistance(@Longitude1 Float, @Latitude1 Float, @Longitude2 Float, @Latitude2 Float)
Returns Float
AS
Begin
Declare @DeltaX Float
Declare @DeltaY Float
Declare @DeltaXMeters Float
Declare @DeltaYMeters Float
Declare @MetersPerDegreeLong Float
Declare @CenterY Float
Declare @gEARTH_CIRCUM_METRES Float
Set @gEARTH_CIRCUM_METRES = 6378007 * 2 * 3.14159265
Set @DeltaX = Abs(@Longitude2 - @Longitude1)
Set @DeltaY = Abs(@Latitude2 - @Latitude1)
Set @CenterY = (@Latitude1 + @Latitude2) / 2
Set @MetersPerDegreeLong = (Cos(@CenterY * (3.14159265 / 180)) * @gEARTH_CIRCUM_METRES) / 360
Set @DeltaXMeters = @DeltaX * @MetersPerDegreeLong
Set @DeltaYMeters = @DeltaY * 111113.519
Return Sqrt(@DeltaXMeters * @DeltaXMeters + @DeltaYMeters * @DeltaYMeters) / 1609.344
End
-George
Strong and bitter words indicate a weak cause. - Fortune cookie wisdom