Re: Finding nearest numeric value

From: Peter Fein <pfein(at)pobox(dot)com>
To: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
Cc: Poul Møller Hansen <freebsd(at)pbnet(dot)dk>, pgsql-general(at)postgresql(dot)org
Subject: Re: Finding nearest numeric value
Date: 2005-08-17 14:42:29
Message-ID: 43034CD5.5090901@pobox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sean Davis wrote:
> On 8/17/05 8:50 AM, "Poul Møller Hansen" <freebsd(at)pbnet(dot)dk> wrote:
>
>
>>Does anyone know how to find the row with the nearest numeric value, not
>>necessarily an exact match ?
>
>
> To find the nearest value in number_column to some CONSTANT (where you
> replace constant with a number), try:
>
> select *,(number_column - CONSTANT)^2 as d from tablename order by d limit
> 1;
>

Save yourself some cycles - use abs() instead of exponentiation.

--
Peter Fein pfein(at)pobox(dot)com 773-575-0694

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2005-08-17 15:06:55 Re: Postgresql server "crash" after some day
Previous Message Jim C. Nasby 2005-08-17 14:41:20 Re: How to implement table caching