Subject Re: First word after in alphabetical order
Author Svein Erling Tysvær
I doubt it helps, but you could try

SELECT N1.ID_NAME
FROM NAMES N1
WHERE N1.NAME>='FISHE'
AND NOT EXISTS(SELECT * FROM NAMES N2
JOIN NAMES N3 ON N3.NAME < N2.NAME
OR (N3.NAME = N2.NAME AND N3.ID_NAME < N2.ID_NAME)
JOIN NAMES N4 ON N4.NAME < N3.NAME
OR (N4.NAME = N3.NAME AND N4.ID_NAME < N3.ID_NAME)
JOIN NAMES N5 ON N5.NAME < N4.NAME
OR (N5.NAME = N4.NAME AND N5.ID_NAME < N4.ID_NAME)
JOIN NAMES N6 ON N6.NAME < N5.NAME
OR (N6.NAME = N5.NAME AND N6.ID_NAME < N5.ID_NAME)
JOIN NAMES N7 ON N7.NAME < N6.NAME
OR (N7.NAME = N6.NAME AND N7.ID_NAME < N6.ID_NAME)
JOIN NAMES N8 ON N8.NAME < N7.NAME
OR (N8.NAME = N7.NAME AND N8.ID_NAME < N7.ID_NAME)
JOIN NAMES N9 ON N9.NAME < N8.NAME
OR (N9.NAME = N8.NAME AND N9.ID_NAME < N8.ID_NAME)
JOIN NAMES N10 ON N1.NAME < N9.NAME
OR (N1.NAME = N9.NAME AND N10.ID_NAME < N9.ID_NAME)
JOIN NAMES N11 ON N11.NAME < N10.NAME
OR (N11.NAME = N10.NAME AND N11.ID_NAME < N10.ID_NAME)
JOIN NAMES N12 ON N12.NAME < N11.NAME
OR (N12.NAME = N11.NAME AND N12.ID_NAME < N11.ID_NAME)
WHERE (N2.NAME < N1.NAME
OR (N2.NAME = N1.NAME AND N2.ID_NAME < N1.ID_NAME))
AND N12.NAME >= 'FISHE')

My guess is that this is poor when there is a lot of records with NAME
>= 'FISHE'

Set

--- In firebird-support@yahoogroups.com, "ericsibert1971" wrote:
> Hi,
>
> I have a table with a lot of names in a colomn. Giving the beginning
> of a word, I want to be able to go to the corresponding area in the
> table and to have an overview of the area. More precisely, I'm
> looking for FISHE. I want to retrieve the first record in
> alphabetical after FISHE, inclunding FISHE itself if it exists. I
> also want the ten next and ten previous records in alphabetical
> order. Therefore, I tried :
> - for the first and ten next :
> SELECT FIRST 11
> ID_NAME
> FROM NAMES
> WHERE NAME>='FISHE'
> ORDER BY NAME;
>
> -for the ten previous :
> SELECT FIRST 10
> ID_NAME
> FROM NAMES
> WHERE NAME<'FISHE'
> ORDER BY NAME DESC;
>
> But this is very slow. 1m45s each on my server (FB1.5.2 /Linux).
>
> In opposite :
> SELECT FIRST 11
> ID_NAME
> FROM NAMES
> WHERE NAME LIKE 'FISHE%'
> ORDER BY NAME;
>
> only need 1s (but don't give the right result if less than 11 names
> are starting with FISHE).
>
> Any idea to solve my problem (the global one, not just improving my
> requests)?
>
> Eric