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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:02:02+00:00 2026-05-22T16:02:02+00:00

I’m looking for something that, given a table like: | id | number |

  • 0

I’m looking for something that, given a table like:

| id | number |
|  1 |     .7 |
|  2 |   1.25 |
|  3 |   1.01 |
|  4 |    3.0 |

the query SELECT * FROM my_table WHEREnumberCLOSEST(1) would return row 3. I only care about numbers. Right now I’ve got a procedure that just loops over every row and does a comparison, but I figure the information should be available from a b-tree index, so this might be possible as a builtin, but I can’t find any documentation suggesting that it does.

  • 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-22T16:02:03+00:00Added an answer on May 22, 2026 at 4:02 pm

    I may be a little off on the syntax, but this parameterized query (all the ? take the ‘1’ of the original question) should run fast, basically 2 B-Tree lookups [assuming number is indexed].

    SELECT * FROM
    (
      (SELECT id, number FROM t WHERE number >= ? ORDER BY number LIMIT 1) AS above
      UNION ALL
      (SELECT id, number FROM t WHERE number < ? ORDER BY number DESC LIMIT 1) as below
    ) 
    ORDER BY abs(?-number) LIMIT 1;
    

    The query plan for this with a table of ~5e5 rows (with an index on number) looks like this:

    psql => explain select * from (
            (SELECT id, number FROM t WHERE number >= 1 order by number limit 1) 
            union all
            (select id, number from t where number < 1 order by number desc limit 1)
    ) as make_postgresql_happy 
    order by abs (1 - number) 
    limit 1;
                                                      QUERY PLAN
    --------------------------------------------------------------------------------------------------------------
     Limit  (cost=0.24..0.24 rows=1 width=12)
       ->  Sort  (cost=0.24..0.24 rows=2 width=12)
             Sort Key: (abs((1::double precision - public.t.number)))
             ->  Result  (cost=0.00..0.23 rows=2 width=12)
                   ->  Append  (cost=0.00..0.22 rows=2 width=12)
                         ->  Limit  (cost=0.00..0.06 rows=1 width=12)
                               ->  Index Scan using idx_t on t  (cost=0.00..15046.74 rows=255683 width=12)
                                     Index Cond: (number >= 1::double precision)
                         ->  Limit  (cost=0.00..0.14 rows=1 width=12)
                               ->  Index Scan Backward using idx_t on t  (cost=0.00..9053.67 rows=66136 width=12)
                                     Index Cond: (number < 1::double precision)
    (11 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
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 French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.