Subject | Re: [firebird-support] Re: Select first 3 and last 3 records |
---|---|
Author | Kjell Rilbe |
Post date | 2005-06-09T10:47:31Z |
I doubt that the inner queries can contain "order by" anyway.
I suggest you run two queries and "concatenate" them in the client.
Otherwize you can do it like this, although I doubt you will get very
good performance (tested on one of my tables in SQL Server):
select ...
from Ordtrn O1
where (
select count(*)
from Ordtrn O2
where O2.InvNo <= O1.InvNo
) <= 3
or (
select count(*)
from Ordtrn O2
where O2.InvNo >= O1.InvNo
) <= 3
Kjell
Cao Ancoinc wrote:
--------------------------------------
Kjell Rilbe
Adressmarknaden AM AB
E-post: kjell.rilbe@...
Telefon: 08-761 06 55
Mobil: 0733-44 24 64
I suggest you run two queries and "concatenate" them in the client.
Otherwize you can do it like this, although I doubt you will get very
good performance (tested on one of my tables in SQL Server):
select ...
from Ordtrn O1
where (
select count(*)
from Ordtrn O2
where O2.InvNo <= O1.InvNo
) <= 3
or (
select count(*)
from Ordtrn O2
where O2.InvNo >= O1.InvNo
) <= 3
Kjell
Cao Ancoinc wrote:
> Thanks MailMur however--
> as far as I know the From Select statement will only be implemented in Ver 2
> so I am back to my original problem
>
>
>>-----Original Message-----
>>From: firebird-support@yahoogroups.com
>>[mailto:firebird-support@yahoogroups.com]On Behalf Of mailmur
>>Sent: 09 June 2005 09:08
>>To: firebird-support@yahoogroups.com
>>Subject: [firebird-support] Re: Select first 3 and last 3 records
>>
>>>I need to select the first 3 and last 3 records from a transaction
>>
>>file This I would imagine would be a simple query as the example below
>>however Firebird objects to the "order by" clause when a "union"
>>operator is present
>>
>>>select first 3 PrnId, InvNo
>>>from Ordtrn
>>>where PrnId='21'
>>>order by InvNo
>>>union
>>>select first 3 PrnId, InvNo
>>>from Ordtrn
>>>where PrnId='21'
>>>order by InvNo desc
>>
>>Maybe a derived inner queries could solve this problem.
>>Select a.* From
>> (Select first 3 prnid, invno From Ordtrn ... Order by Invno asc) a
>>UNION
>>Select b.* From
>> (Select first 3 prnid, invno From Ordtrn ... Order by Invno desc) b
--------------------------------------
Kjell Rilbe
Adressmarknaden AM AB
E-post: kjell.rilbe@...
Telefon: 08-761 06 55
Mobil: 0733-44 24 64