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 8937913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:30:00+00:00 2026-06-15T10:30:00+00:00

I’ve got two tables called hotels and hotel locations . In the hotels table

  • 0

I’ve got two tables called hotels and hotel locations. In the hotels table I have HotelId, hotelName. In the hotelLocations table I have HotelId, HotelNorthing, HotelEasting. HotelId is a foreign key that references HotelId in the hotels table, HotelNorthing and HotelEasting are UTM co-ordinates that hold the northing and easting values for the hotels.

I would like to find, using this data the 2 hotels that are closest to each other and display with a query the hotel name and id from the hotels table and the distance from the calculation. I previously tried all sorts of cross joins but came up short. I used the Pythagoras theorem to calculate the distance between the hotels but the structure of an appropriate query has eluded me.

  • 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-06-15T10:30:03+00:00Added an answer on June 15, 2026 at 10:30 am

    Assuming MS SQL 2008 or later I’d use the built in geography type:

    insert into locations (name, location) values
    ('Hotel1', 'Point(138.58 -34.92)'),
    ('Hotel2', 'POINT(138.97 -34.78)'),
    ('Hotel3', 'POINT(138.00 -34.00)'),
    ('Hotel4', 'POINT(138.57 -34.01)'),
    ('Hotel5', 'POINT(138.50 -34.03)'),
    ('Hotel6', 'POINT(138.49 -34.04)'),
    ('Hotel7', 'POINT(138.88 -34.04)')
    
    
    declare @StartLocation geography
    select @StartLocation = location
    from Locations 
    where name = 'Hotel1'
    
    select location.STDistance(@StartLocation)
    from locations
    

    This will give you the distance from Hotel1 to all the other hotel’s, then it’s just a matter of sorting the list and taking the top 2.


    EDIT: To calculate from every hotel to every other hotel:

    create table locations
    (
        ID INT IDENTITY(1,1),
        Name varchar(50),
        Location geography
    )
    
    insert into locations (name, location) values
    ('Hotel1', 'Point(138.58 -34.92)'),
    ('Hotel2', 'POINT(138.59 -34.93)'),
    ('Hotel3', 'POINT(138.00 -34.00)'),
    ('Hotel4', 'POINT(138.57 -34.01)'),
    ('Hotel5', 'POINT(138.50 -34.03)'),
    ('Hotel6', 'POINT(138.49 -34.04)'),
    ('Hotel7', 'POINT(138.88 -34.04)')
    
    select l1.name, l2.name, l1.location.STDistance(l2.Location)
    FROM Locations l1
    JOIN Locations l2 on l2.ID < l1.ID
    

    EDIT 2:
    Split Hotel Names out into seperate table:

    create table locationNames
    (
        ID INT IDENTITY(1,1),
        Name VARCHAR(50)
    )
    insert into locationNames (Name) VALUES
    ('Hotel1'),
    ('Hotel2'),
    ('Hotel3'),
    ('Hotel4'),
    ('Hotel5'),
    ('Hotel6'),
    ('Hotel7')
    
    create table locations
    (
        ID INT IDENTITY(1,1),
        Location geography
    )
    
    insert into locations (location) values
    ('Point(138.58 -34.92)'),
    ('POINT(138.59 -34.93)'),
    ('POINT(138.00 -34.00)'),
    ('POINT(138.57 -34.01)'),
    ('POINT(138.50 -34.03)'),
    ('POINT(138.49 -34.04)'),
    ('POINT(138.88 -34.04)')
    
    select ln1.Name, ln2.Name, l1.location.STDistance(l2.Location)
    FROM Locations l1
    JOIN Locations l2 on l2.ID < l1.ID
    JOIN LocationNames ln1 on ln1.ID = l1.ID
    JOIN LocationNames ln2 on ln2.ID = l2.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.