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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:01:33+00:00 2026-05-16T14:01:33+00:00

Here are some example numbers: 987 1001 1004 1009 1010 1016 1020 1050 For

  • 0

Here are some example numbers:

987
1001
1004
1009
1010
1016
1020
1050

For example, I would like select the top 4 numbers that close to the given number 1009 (so, the results would be 1001, 1004, 1010 and 1016), how should I write the SQL expression?

  • 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-16T14:01:33+00:00Added an answer on May 16, 2026 at 2:01 pm

    Get the distance from the given number by subtracting and using the abs function:

    select top 4 Number
    from NumberTable
    where number <> 1009
    order by abs(Number - 1009)
    

    Edit:

    As you now mention that you have a very large table, you would need a way to elliminate most of the results first. You could pick the four closest in both direction and then get the correct ones from that:

    select top 4 Number
    from (
      select Number
      from (
        select top 4 Number
        from NumberTable
        where number < 1009
        order by number desc
      )
      union all
      select Number
      from (
        select top 4 Number
        from NumberTable
        where number > 1009
        order by number
      )
    )
    order by abs(Number - 1009)
    

    If the numbers are evenly distributed so that you are sure that you can find the numbers in a range like for example +-100 numbers, you can simply get that range first:

    select top 4 Number
    from (
      select Number
      from NumberTable
      where number between 1009-100 and 1009+100
    )
    where number <> 1009
    order by abs(Number - 1009)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have html like so: <div class=foo> (<a href=forum.example.com>forum</a>) <p> Some html here.... </div>
Here's some example code: class Obj attr :c, true def == that p '=='
Here is some example data: data = data.frame(series = c(1a, 1b, 1e), reading =
Just some example code here, but I have lists of strings that I want
I am developing a application using PHP. Some example code is here. $url =
For example (in my case) here's some code, <?php woo_post_inside_before(); if ( $woo_options['woo_post_content'] !=
Here's an example I saw for some GLX code: display = XOpenDisplay(0); // ...
I'm dealing with some gnarly HTML. Here's a stylized example of the ancestry of
I'm working on a stored proc that executes some dynamic sql. Here's the example
I have string like 8.123.351 (Some text here) I have used the Regex /([0-9,]+(\.[0-9]{2,})+(\.[0-9]{2,})?)/

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.