Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 0DDE49FB2BA for ; Mon, 2 Oct 2006 21:10:40 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 81945-06 for ; Tue, 3 Oct 2006 00:10:36 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from wolff.to (wolff.to [66.93.197.194]) by postgresql.org (Postfix) with SMTP id A9D549FB2AA for ; Mon, 2 Oct 2006 21:10:35 -0300 (ADT) Received: (qmail 1531 invoked by uid 500); 3 Oct 2006 00:20:49 -0000 Date: Mon, 2 Oct 2006 19:20:49 -0500 From: Bruno Wolff III To: Alexander Ilyin Cc: pgsql-sql@postgresql.org Subject: Re: How to FindNearest Message-ID: <20061003002049.GA1490@wolff.to> Mail-Followup-To: Bruno Wolff III , Alexander Ilyin , pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.1i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200610/9 X-Sequence-Number: 26398 On Sat, Sep 30, 2006 at 11:43:40 +0300, Alexander Ilyin 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 can input in TextBox some letters. OnChange of this TextBox we need to make current row selection in the Grid on the row 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.