Thread: How to FindNearest

How to FindNearest

From
"Alexander Ilyin"
Date:
How to implement the functionality similar to the BDE/Paradox TTable.FindNearest?

Suppose we have huge table. On the screen we need to show contents of that table in the Grid and under that Grid user
caninput in TextBox some letters. OnChange of this TextBox we need to make current row selection in the Grid on the row
withvalue of some column nearest to the user input in the TextBox.
 

How this can be implemented in PostgreSQL?

At my first sight:
I found there is CURSOR functionality which can move that cursor through the ResultSet. But we can move it just
relativelyphisical row number. Is there any chance to make cursor move to the row by some rule based on the row
values?

All possible approaches and ideas highly appreciated. There is just one requirement - the performance, it should work
asfast as possible.
 

Regards,
Alexander Ilyin


Re: How to FindNearest

From
Bruno Wolff III
Date:
On Sat, Sep 30, 2006 at 11:43:40 +0300, Alexander Ilyin <a_ilyin@ukr.net> wrote:
> 
> Suppose we have huge table. On the screen we need to show contents of that table in the Grid and under that Grid user
caninput in TextBox some letters. OnChange of this TextBox we need to make current row selection in the Grid on the row
withvalue of some column nearest to the user input in the TextBox.
 
> 
> How this can be implemented in PostgreSQL?

You can search for the row with the closest above and the row with the
closest below and then return whichever these is closer. (Remember to handle
the case where the target is larger or smaller than all values in the
database.) You can use ORDER BY and LIMIT 1 and an appropiiate WHERE
clause (something like columnname >= targetvalue) to do this.


Re: How to FindNearest

From
"Alexander Ilyin"
Date:
-----Original Message-----
From: Bruno Wolff III <bruno@wolff.to>
To: Alexander Ilyin <a_ilyin@ukr.net>
Date: Mon, 2 Oct 2006 19:20:49 -0500
Subject: Re: How to FindNearest

> On Sat, Sep 30, 2006 at 11:43:40 +0300,
>   Alexander Ilyin <a_ilyin@ukr.net> wrote:
> > 
> > Suppose we have huge table. On the screen we need to show contents of that table in the Grid and under that Grid
usercan input in TextBox some letters. OnChange of this TextBox we need to make current row selection in the Grid on
therow with value of some column nearest to the user input in the TextBox.
 
> > 
> > How this can be implemented in PostgreSQL?
> 
> You can search for the row with the closest above and the row with the
> closest below and then return whichever these is closer. (Remember to handle
> the case where the target is larger or smaller than all values in the
> database.) You can use ORDER BY and LIMIT 1 and an appropiiate WHERE
> clause (something like columnname >= targetvalue) to do this.
> 
>    
Thank you for your great idea. But how it can be used for positioning the cursor in the already existed ResultSet?
Usingyour idea I can found the closest to targetvalue row but not its position in my ResultSet.
 
Anyway thank you for your idea it is very useful by itself. Also I can solve my problem using your idea and emulating
themovement in my existed ResultSet. Even better - no need to store the large RS between cursor movements. Just each
timeI need to fetch the all visible rows.
 





Re: How to FindNearest

From
Bruno Wolff III
Date:
On Tue, Oct 03, 2006 at 14:43:40 +0300, Alexander Ilyin <a_ilyin@ukr.net> wrote:
> 
> Thank you for your great idea. But how it can be used for positioning the cursor in the already existed ResultSet?
Usingyour idea I can found the closest to targetvalue row but not its position in my ResultSet.
 

You wouldn't be able to use it to position a cursor. But if you aren't
retrieving a lot of records at once, this may still be a workable strategy
for you.

> Anyway thank you for your idea it is very useful by itself. Also I can solve my problem using your idea and emulating
themovement in my existed ResultSet. Even better - no need to store the large RS between cursor movements. Just each
timeI need to fetch the all visible rows.
 

That sounds pretty reasonable.