Re: Finding nearest numeric value

Поиск
Список
Период
Сортировка
От Csaba Nagy
Тема Re: Finding nearest numeric value
Дата
Msg-id 1124292936.24337.105.camel@coppola.muc.ecircle.de
обсуждение исходный текст
Ответ на Re: Finding nearest numeric value  (Richard Huxton <dev@archonet.com>)
Ответы Re: Finding nearest numeric value  (Bruno Wolff III <bruno@wolff.to>)
Re: Finding nearest numeric value  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
The only problem is that you can't use the order by/limit syntax inside
the union queries I guess, cause the query you proposed is giving a
syntax error. I also thought first to do it like this, but it won't
work. If it would, then you could wrap the thing in another query which
orders by the difference and limits to the first one ;-)

Cheers,
Csaba.

On Wed, 2005-08-17 at 17:10, 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.


В списке pgsql-general по дате отправления:

Предыдущее
От: Sean Davis
Дата:
Сообщение: Re: Finding nearest numeric value
Следующее
От: Csaba Nagy
Дата:
Сообщение: Re: Finding nearest numeric value