Search Postgresql Archives

Re: Finding nearest numeric value

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

--
  Richard Huxton
  Archonet Ltd


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux