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

  • SEARCH
  • Home
  • 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 7168047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:42:00+00:00 2026-05-28T14:42:00+00:00

I have 2 tables: tZipCodeNoCity with ZipCode and PointGeography and MBLPosition with Latitude and

  • 0

I have 2 tables:

tZipCodeNoCity with ZipCode and PointGeography
and MBLPosition with Latitude and Longitude

In this query I’m finding closest ZipCode to my positions. It’s “poor mans” geocoding 🙂

How do I write this query so I don’t have to do this SELECT TOP 1 inline?
It’s pretty slow with even 150 devices (like 20 seconds)

I had to include 150 mile radius into this subselect to get it faster

SELECT LastPositions.DeviceId, P.Description, P.Latitude, P.Longitude, P.Speed, P.DeviceTime,
(
SELECT TOP 1 ZC.ZipCode
FROM dbo.tZipCodeNoCity ZC
WHERE ZC.PointGeography.STDistance(geography::STPointFromText('POINT(' + CAST(P.Longitude AS VARCHAR(20)) + ' ' + CAST(P.Latitude AS VARCHAR(20)) + ')', 4326)) < 150 * 1609.344
ORDER BY ZC.PointGeography.STDistance(geography::STPointFromText('POINT(' + CAST(P.Longitude AS VARCHAR(20)) + ' ' + CAST(P.Latitude AS VARCHAR(20)) + ')', 4326))
) 
            FROM dbo.MBLPosition P
            INNER JOIN 
            (
                SELECT D.DeviceId, MAX(P.PositionKey) LastPositionKey 
                FROM dbo.MBLPosition P
                INNER JOIN IDATTApplication.dbo.MBLDevice D ON P.DeviceKey = D.DeviceKey
                GROUP BY D.DeviceId
            ) LastPositions ON P.PositionKey = LastPositions.LastPositionKey
  • 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-28T14:42:01+00:00Added an answer on May 28, 2026 at 2:42 pm

    In a project I worked on about 12 years ago, I ran a query along these lines to reduce the list of possibilities before doing the actual distance calculation:

    WHERE zip.lat < my.lat + 0.5 && zip.lat > my.lat - 0.5 
        && zip.long < my.long + 0.5 && zip.long > my.long - 0.5
    

    From that subset, I calculate the actual distance between the two points and sort on it. You’ll have to adjust the “0.5” portion as appropriate to get a big enough box to be sure you’re going to get a hit.

    And I would imagine that there’s a better way than STPointFromText to create your point object. Could you use STPointFromWKB? Could you convert to the geography type once?

    See this page for an example of creating your point via SET.

    DECLARE @p geography;
    SET @p = geography::STGeomFromText('POINT(' + CAST(P.Longitude AS VARCHAR(20)) + ' ' + CAST(P.Latitude AS VARCHAR(20)) + ')', 4326);
    
    SELECT TOP 1 ZC.ZipCode
    FROM dbo.tZipCodeNoCity ZC
    WHERE ZC.PointGeography.STDistance(@p)) < 150 * 1609.344
    ORDER BY ZC.PointGeography.STDistance(@p))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have three tables: page, attachment, page-attachment I have data like this: page ID
I have tables & data like this: venues table contains : id +----+---------+ |
I have tables as posted in this question . •Measurements (MeasureID, Time, Distance, Value)
I have tables like this fake ones: data: id type month value 1 1
I have tables like this License ------------ Id LicenseName DriversLicense ------------- Id LicenseId DriverId
I have tables like this: Table1 Table2 name1 | link_id name2 | link_id text
I have tables A and B and A_has_B where have this fields: A: id,
I have this Jtree, and I have to insert into it tables in my
I have tables in a MySQL database like this... CREATE TABLE `someUserTable` ( userId

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.