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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:36:43+00:00 2026-06-04T04:36:43+00:00

I’ve got a really complicated query that I need do but I just can’t

  • 0

I’ve got a really complicated query that I need do but I just can’t seem to get my head around how to accomplish it so I’m hoping someone can help me.

I have two queries which I need to make into one query.

My first query is this:

select * from table1 t1 
join table2 t2 on t2.outcode = t1.post_code

This produces results which include LAT and LONG of the postcode like so:

  Name    Postcode          Lat         Long
Taylor         CB8    53.829517    -1.780320

These are just dummy values for test purposes.

My next query is this

SELECT * FROM (
    SELECT t3.location_name, 
    calc_distance(t3.lat, t3.lng, t2.lat,t2.lng) as distance
    FROM table3 t3, table2 t2 
    where t2.outcode = :postcode
    order by distance) i 
where rownum = 1

calc_distance is a function that calculates the distance based on LAT & LONG of a point

If I substitute :postcode with CB8 it produces these results

Location_name     Distance
  NR Location         56.6

What I somehow need to do is produce the following output from a single query.

  Name    Postcode          Lat         Long    Nearest_Loc     Distance
Taylor         CB8    53.829517    -1.780320    NR Location         56.6

I can’t for the life of me work out how to produce this if it is possible.

Can anyone help?

  • 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-04T04:36:43+00:00Added an answer on June 4, 2026 at 4:36 am

    You can use ROW_NUMBER() effectively here. By Partioning on t2.outcode and sorting by distance we can find the smallest distance for each outcode (t3.rn = 1).

    SELECT 
           t1.Name,
           t1.Postcode,
           t2.Lat,         
           t2.Long,    
           t3.Nearest_Loc,
           t3.Distance
    From 
          table1 t1 
           INNER join table2 t2 on t2.outcode = t1.post_code
        LEFT JOIN (
                   SELECT t3.location_name, 
                         calc_distance(t3.lat, t3.lng, t2.lat,t2.lng) as distance,
                         row_number() over (partition by t2.outcode 
                                            order by calc_distance(t3.lat, t3.lng, t2.lat,t2.lng) 
                                          ) rn,
                        t2.outcode
                   FROM table3 t3, table2 t2 
                ) t3
         on t1.Postcode =  t3.PostCode
          and t3.rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't

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.