Subject Re: Select first 3 and last 3 records
Author Svein Erling Tysvær
Well Cao, you could do

select a.PrnId, a.InvNo
from Ordtrn a
where a.PrnId = 21
and (not exists(select * from Ordtrn b
join Ordtrn c on c.PrnId = b.PrnId and c.InvNo < b.InvNo
join Ordtrn d on d.PrnId = c.PrnId and d.InvNo < c.InvNo
where b.PrnId = a.PrnId and b.InvNo < a.InvNo)
or not exists(select * from Ordtrn b
join Ordtrn c on c.PrnId = b.PrnId and c.InvNo > b.InvNo
join Ordtrn d on d.PrnId = c.PrnId and d.InvNo > c.InvNo
where b.PrnId = a.PrnId and b.InvNo > a.InvNo))

Hope I got this correct, haven't got time to check, but you should get
the idea,
Set

--- In firebird-support@yahoogroups.com, "Cao Ancoinc" wrote:
> Hi all
>
> 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
>
> Any suggestions on how to work around the problem
>
> Regards Cao