Subject | Re: [firebird-support] Getting the 'closest' return |
---|---|
Author | Dimitry Sibiryakov |
Post date | 2004-06-09T07:43:36Z |
On 9 Jun 2004 at 3:43, dleec45 wrote:
CREATE PROCEDURE (A_VALUE INTEGER)
RETURNING (whatever_you_want) AS
DECLARE VARIABLE CLOSEST_FROM_BOTTOM INTEGER;
DECLARE VARIABLE CLOSEST_FROM_TOP INTEGER;
BEGIN
SELECT MAX(THE_FIELD) WHERE THE_FIELD<=:A_VALUE INTO
:CLOSEST_FROM_BOTTOM;
SELECT MIN(THE_FIELD) WHERE THE_FIELD>:A_VALUE INTO
:CLOSEST_FROM_TOP;
/* Here you do whatever you wand and select whatever you want */
END^
SY, Dimitry Sibiryakov.
>How do I write a query which will return the closest value in theIMHO, the simplest way - stored procedure. Something like this:
>where statement. If I have a bunch of numbers in a table that has
>values like 100, 200, 300, 400 and my where clause looks for 249 or
>250, how do I return 200 or 200 and 300, whatever the case may be?
CREATE PROCEDURE (A_VALUE INTEGER)
RETURNING (whatever_you_want) AS
DECLARE VARIABLE CLOSEST_FROM_BOTTOM INTEGER;
DECLARE VARIABLE CLOSEST_FROM_TOP INTEGER;
BEGIN
SELECT MAX(THE_FIELD) WHERE THE_FIELD<=:A_VALUE INTO
:CLOSEST_FROM_BOTTOM;
SELECT MIN(THE_FIELD) WHERE THE_FIELD>:A_VALUE INTO
:CLOSEST_FROM_TOP;
/* Here you do whatever you wand and select whatever you want */
END^
SY, Dimitry Sibiryakov.