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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:16:07+00:00 2026-06-15T22:16:07+00:00

Hi I have a database with the following structure: Table 1: time(type timestamp) longitude(type

  • 0

Hi I have a database with the following structure:

Table 1:

time(type timestamp)        longitude(type string)        latitude(type string)
2:00                        -110.4365                     38.7463
2:00                        -110.2743                     38.7983
2:00                        -102.4434                     36.9438
3:00                        -112.3254                     39.2222
etc                         etc                           etc

Now I have some code that plots values and when it does so it loses some accuracy.The user can click the points and query the database for further information on the point. However as the plotting function lost some accuracy I need to account for this in the query.

So the idea is that I would like to query for time = x and also (latitude = closest value to y and longitude = closest value to z). You see the closest value is dependent on both latitude and longitude and this is where I’m getting confused as to how to proceed.

Lets say I was looking for:

Time = 2:00
Longitude = -110.3421
Latitude = 38.7587

Now if I did a query for:

Time = 2:00 and Longitude = closest to -110.3421

then the result would be row 2.

However if I did:

Time = 2:00 and Latitude = closest to 38.7587

then the result would be row 1.

If I do a query on:

Time = 2:00 and Longitude = closest to -110.3421 and Latitude = closest to 38.7587

then the result would be row 1. This is the type of query that I require…

I found the following post useful for a query on the closest value in one field:

https://stackoverflow.com/a/6103352/1800665

Thanks,
James

EDIT: I have the following:

(SELECT * FROM (
(SELECT * FROM Table1 WHERE cast(latitude as double precision) >= '38.7587' AND healthtime = '2:00' AND cast(longitude as double precision) >= '-110.3421') 
UNION ALL 
(SELECT * FROM Table1 WHERE cast(latitude as double precision) < '38.7587' AND healthtime = '2:00' AND cast(longitude as double precision) < '-110.3421')
) as nearest ORDER BY ( abs('38.7587'-cast(latitude as double precision)) + abs('-110.3421'-cast(longitude as double precision))) LIMIT 1)
  • 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-15T22:16:08+00:00Added an answer on June 15, 2026 at 10:16 pm

    You can try something like this:

    SELECT *
    FROM table1
    WHERE healthtime = '2:00'
    ORDER BY ((longitude::double precision - (-110.3421))^2 +
              (latitude::double precision -  (38.7587  ))^2)
    LIMIT 1
    

    It will return you the point with minimum “distance” : ( (lon-lon1)^2 + (lat-lat1)^2 -> min ) .

    My SQLFiddle with example.

    UPD Test query:

    SELECT *, ((longitude::double precision - (-110.3421))^2 +
               (latitude::double precision -  (38.7587  ))^2) as distance,
              (abs(longitude::double precision - (-110.3421)) +
               abs(latitude::double precision -  (38.7587  ))) as distance2
    FROM table1
    WHERE time = '2:00'
    ORDER BY ((longitude::double precision - (-110.3421))^2 +
              (latitude::double precision -  (38.7587  ))^2)
    
    LIMIT 2
    

    Gives 2 best results:

    | TIME | LONGITUDE | LATITUDE |   DISTANCE | DISTANCE2 |
    --------------------------------------------------------
    | 2:00 | -110.2743 |  38.7983 |   0.006165 |    0.1074 |
    | 2:00 | -110.4365 |  38.7463 | 0.00906512 |    0.1068 |
    

    DISTANCE = (lon-lon1)^2 + (lat-lat1)^2 (my variant).

    DISTANCE2 = abs(lon-lon1) + abs(lat-lat1) (your variant).

    Your variant gives different “closest”. You can use any variant of “distance” calculation, but I prefer “classical” way – root((lon-lon1)^2 + (lat-lat1)^2).

    New SQLFiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no control over database schema and have the following (simplified) table structure:
I have MySQL table with the following structure: +---------+--------------+------+-----+---------+----------------+ | Field | Type |
I have the following database structure: CREATE TABLE LookupTable ( PK UNIQUEIDENTIFIER PRIMARY KEY,
I have a tree-style database with the following structure: Table fields: NodeID int ParentID
I have the following database structure: Table 1 Table 2 Table 3 tid_1 ----(many-to-one)----
I have two database tables with the following structure: actions: action_id int(11) primary key
I have the following database structure. ID | Name | Marks I am trying
I have the following database table object: public class Goal { @DatabaseField(generatedId = true)
I have the following database design: Employee Table: Username, Name, DivisionCode Division Table: SapCode,
I have the following database design: Employee Table: Username, Name Quiz Table: QuizID, Title,

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.