Re: Finding nearest numeric value - Mailing list pgsql-general

From Peter Fein
Subject Re: Finding nearest numeric value
Date
Msg-id 43038936.1030205@pobox.com
Whole thread Raw
In response to Re: Finding nearest numeric value  (Richard Huxton <dev@archonet.com>)
List pgsql-general
Richard Huxton wrote:
> Poul Møller Hansen wrote:
>
>> Does anyone know how to find the row with the nearest numeric value,
>> not necessarily an exact match ?
>
>
> While the other answers all do their job, and in one go too, I'd be
> surprised if you found anything faster than:
>
> SELECT myval FROM mytable WHERE myval > 1234 ORDER BY myval LIMIT 1
> UNION ALL
> SELECT myval FROM mytable WHERE myval < 1234 ORDER BY myval DESC LIMIT 1
>
> That gives you (up to) two values to look at, but should use any index
> you have on myval.
>
> You can always sort the results by abs(myval) then if you don't want to
> handle two values in the application layer.
>

Ahh, should that be >= and <= ? ;)

--
Peter Fein                 pfein@pobox.com                 773-575-0694

Basically, if you're not a utopianist, you're a schmuck. -J. Feldman

pgsql-general by date:

Previous
From: Sven Willenberger
Date:
Subject: Re: PostgreSQL 8.0.3 limiting max_connections to 64 ?
Next
From: Dr NoName
Date:
Subject: COMMIT in ps output